Noirand icon

cs script test 01

Noirand | PRO | 04/14/16 11:23:24 AM UTC | 0 ⭐ | 248 👁️ | Never ⏰ | []
C# |

5.03 KB

|

None

|

0 👍

/

0 👎

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Loaders;
 
public class FirstTest : MonoBehaviour {
//============================================================
// 変数宣言
//============================================================
    //--------------------------------------------------
    // public
    //--------------------------------------------------
    [MoonSharpUserData]
    class MyLua {
        FirstTest m_Parent;
        public MyLua(FirstTest parent)
        {
            m_Parent = parent;
        }
 
        public void TimeWait()
        {
            if (m_Parent != null)
                m_Parent.SetTimeWait();
        }
        public void KeyWait()
        {
            if (m_Parent != null)
                m_Parent.SetKeyWait();
        }
        public void NoWait()
        {
            if (m_Parent != null)
                m_Parent.SetNoWait();
        }
    }
 
    public enum EZ_WAIT_KEY
    {
        _NONE   // 待たない
        ,TIME   // 時間待ち
        ,KEY    // キー入力待ち
    }
 
    public EZ_WAIT_KEY  WaitKey     { get; private set; }
    public void SetNoWait()     { WaitKey = EZ_WAIT_KEY._NONE; }
    public void SetTimeWait()   { WaitKey = EZ_WAIT_KEY.TIME; }
    public void SetKeyWait()    { WaitKey = EZ_WAIT_KEY.KEY; }
 
    //--------------------------------------------------
    // private
    //--------------------------------------------------
    private Text    m_Text;
    private string  m_CurMsg;
    private int     m_iNum;
 
    // lua 用クラス
    private Script      m_Lua;
    private DynValue    m_Func;
    private DynValue    m_LuaCoroutine;
 
//============================================================
// 関数定義
//============================================================
    //--------------------------------------------------
    // コンストラクタ
    //--------------------------------------------------
    void Awake()
    {
        UserData.RegisterAssembly();
 
        m_Text  = GetComponent<Text>();
        m_Lua   = new Script();
 
        WaitKey = EZ_WAIT_KEY._NONE;
 
        #if false
        Dictionary<string, string> scripts = new  Dictionary<string, string>();
 
        object[] result = Resources.LoadAll("Lua", typeof(TextAsset));
 
        foreach(TextAsset ta in result.OfType<TextAsset>())
        {
            scripts.Add(ta.name, ta.text);
        }
 
        Script.DefaultOptions.ScriptLoader = new MoonSharp.Interpreter.Loaders.UnityAssetsScriptLoader(scripts);
        #endif
    }
    //--------------------------------------------------
    // 最初の更新
    //--------------------------------------------------
    void Start()
    {
        ((ScriptLoaderBase)m_Lua.Options.ScriptLoader).ModulePaths = //ScriptLoaderBase.UnpackStringPaths(Application.dataPath + "/Resources");
            ScriptLoaderBase.UnpackStringPaths(UnityAssetsScriptLoader.DEFAULT_PATH + "/?");
 
        m_Lua.Globals["obj"] = new MyLua(this);
 
        var ret = m_Lua.DoFile("test");//Script.RunFile("test");
        Debug.Log( ret.Table );
        if (ret.Table != null)
        {
            foreach (DynValue pKey in ret.Table.Keys)
            {
                Debug.Log(pKey + "," + ret.Table.Get(pKey));
                m_Text.text += ret.Table.Get(pKey) + "\n";
            }
 
            //Debug.Log(ret.Table.Values);
            //Debug.Log( m_Lua.Globals.Get("req").String );
        }
 
        TryLuaCoroutine();
    }
    //--------------------------------------------------
    // 更新処理
    //--------------------------------------------------
    void Update()
    {
    
    }
    //---------------------------------------------------
    // Lua のコルーチンてすと
    //---------------------------------------------------
    void TryLuaCoroutine()
    {
        m_Func = m_Lua.DoFile("adv");
        m_LuaCoroutine = m_Lua.CreateCoroutine(m_Func);
        /*
        foreach (DynValue x in m_LuaCoroutine.Coroutine.AsTypedEnumerable())
        {
            //DynValue x = coroutine.Coroutine.Resume();
            m_Text.text += x + "\n";
            Debug.Log(x);
        }
*/      
        StartCoroutine(ShowDialog());
    }
    //---------------------------------------------------
    // Lua のコルーチンを受け取る C# コルーチン
    //---------------------------------------------------
    IEnumerator ShowDialog()
    {
        foreach (DynValue x in m_LuaCoroutine.Coroutine.AsTypedEnumerable())
        {
            //DynValue x = coroutine.Coroutine.Resume();
            if (x.IsNotVoid())
            {
                //m_Text.text = x.CastToString();
                m_CurMsg = "";
 
                m_iNum = 0;
                var textPut = StartCoroutine( PutText(x.CastToString()) );
                //m_Text.text = m_CurMsg;
 
                Debug.Log(x);
 
                yield return textPut;
 
                switch (WaitKey)
                {
                    case EZ_WAIT_KEY._NONE:
                        yield return null;
                        break;
                    case EZ_WAIT_KEY.TIME:
                        yield return new WaitForSeconds(3);
                        break;
                    case EZ_WAIT_KEY.KEY:
                        yield return new WaitUntil(() => Input.anyKeyDown);
                        break;
                }
            }
        }
    }
    //--------------------------------------------------
    // メッセージを1文字ずつ表示するコルーチン
    //--------------------------------------------------
    IEnumerator PutText(string sText)
    {
        int iNum = 0;
 
        while (iNum < sText.Length)
        {
            m_CurMsg = sText.Substring(0, ++iNum);
 
            m_Text.text = m_CurMsg;
 
            yield return new WaitForSeconds(0.1f);
        }
    }

Comments