Noirand

cs script test 01

Apr 14th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using MoonSharp.Interpreter;
  8. using MoonSharp.Interpreter.Loaders;
  9.  
  10. public class FirstTest : MonoBehaviour {
  11. //============================================================
  12. // 変数宣言
  13. //============================================================
  14.     //--------------------------------------------------
  15.     // public
  16.     //--------------------------------------------------
  17.     [MoonSharpUserData]
  18.     class MyLua {
  19.         FirstTest m_Parent;
  20.         public MyLua(FirstTest parent)
  21.         {
  22.             m_Parent = parent;
  23.         }
  24.  
  25.         public void TimeWait()
  26.         {
  27.             if (m_Parent != null)
  28.                 m_Parent.SetTimeWait();
  29.         }
  30.         public void KeyWait()
  31.         {
  32.             if (m_Parent != null)
  33.                 m_Parent.SetKeyWait();
  34.         }
  35.         public void NoWait()
  36.         {
  37.             if (m_Parent != null)
  38.                 m_Parent.SetNoWait();
  39.         }
  40.     }
  41.  
  42.     public enum EZ_WAIT_KEY
  43.     {
  44.         _NONE   // 待たない
  45.         ,TIME   // 時間待ち
  46.         ,KEY    // キー入力待ち
  47.     }
  48.  
  49.     public EZ_WAIT_KEY  WaitKey     { get; private set; }
  50.     public void SetNoWait()     { WaitKey = EZ_WAIT_KEY._NONE; }
  51.     public void SetTimeWait()   { WaitKey = EZ_WAIT_KEY.TIME; }
  52.     public void SetKeyWait()    { WaitKey = EZ_WAIT_KEY.KEY; }
  53.  
  54.     //--------------------------------------------------
  55.     // private
  56.     //--------------------------------------------------
  57.     private Text    m_Text;
  58.     private string  m_CurMsg;
  59.     private int     m_iNum;
  60.  
  61.     // lua 用クラス
  62.     private Script      m_Lua;
  63.     private DynValue    m_Func;
  64.     private DynValue    m_LuaCoroutine;
  65.  
  66. //============================================================
  67. // 関数定義
  68. //============================================================
  69.     //--------------------------------------------------
  70.     // コンストラクタ
  71.     //--------------------------------------------------
  72.     void Awake()
  73.     {
  74.         UserData.RegisterAssembly();
  75.  
  76.         m_Text  = GetComponent<Text>();
  77.         m_Lua   = new Script();
  78.  
  79.         WaitKey = EZ_WAIT_KEY._NONE;
  80.  
  81.         #if false
  82.         Dictionary<string, string> scripts = new  Dictionary<string, string>();
  83.  
  84.         object[] result = Resources.LoadAll("Lua", typeof(TextAsset));
  85.  
  86.         foreach(TextAsset ta in result.OfType<TextAsset>())
  87.         {
  88.             scripts.Add(ta.name, ta.text);
  89.         }
  90.  
  91.         Script.DefaultOptions.ScriptLoader = new MoonSharp.Interpreter.Loaders.UnityAssetsScriptLoader(scripts);
  92.         #endif
  93.     }
  94.     //--------------------------------------------------
  95.     // 最初の更新
  96.     //--------------------------------------------------
  97.     void Start()
  98.     {
  99.         ((ScriptLoaderBase)m_Lua.Options.ScriptLoader).ModulePaths = //ScriptLoaderBase.UnpackStringPaths(Application.dataPath + "/Resources");
  100.             ScriptLoaderBase.UnpackStringPaths(UnityAssetsScriptLoader.DEFAULT_PATH + "/?");
  101.  
  102.         m_Lua.Globals["obj"] = new MyLua(this);
  103.  
  104.         var ret = m_Lua.DoFile("test");//Script.RunFile("test");
  105.         Debug.Log( ret.Table );
  106.         if (ret.Table != null)
  107.         {
  108.             foreach (DynValue pKey in ret.Table.Keys)
  109.             {
  110.                 Debug.Log(pKey + "," + ret.Table.Get(pKey));
  111.                 m_Text.text += ret.Table.Get(pKey) + "\n";
  112.             }
  113.  
  114.             //Debug.Log(ret.Table.Values);
  115.             //Debug.Log( m_Lua.Globals.Get("req").String );
  116.         }
  117.  
  118.         TryLuaCoroutine();
  119.     }
  120.     //--------------------------------------------------
  121.     // 更新処理
  122.     //--------------------------------------------------
  123.     void Update()
  124.     {
  125.    
  126.     }
  127.     //---------------------------------------------------
  128.     // Lua のコルーチンてすと
  129.     //---------------------------------------------------
  130.     void TryLuaCoroutine()
  131.     {
  132.         m_Func = m_Lua.DoFile("adv");
  133.         m_LuaCoroutine = m_Lua.CreateCoroutine(m_Func);
  134.         /*
  135.         foreach (DynValue x in m_LuaCoroutine.Coroutine.AsTypedEnumerable())
  136.         {
  137.             //DynValue x = coroutine.Coroutine.Resume();
  138.             m_Text.text += x + "\n";
  139.             Debug.Log(x);
  140.         }
  141. */     
  142.         StartCoroutine(ShowDialog());
  143.     }
  144.     //---------------------------------------------------
  145.     // Lua のコルーチンを受け取る C# コルーチン
  146.     //---------------------------------------------------
  147.     IEnumerator ShowDialog()
  148.     {
  149.         foreach (DynValue x in m_LuaCoroutine.Coroutine.AsTypedEnumerable())
  150.         {
  151.             //DynValue x = coroutine.Coroutine.Resume();
  152.             if (x.IsNotVoid())
  153.             {
  154.                 //m_Text.text = x.CastToString();
  155.                 m_CurMsg = "";
  156.  
  157.                 m_iNum = 0;
  158.                 var textPut = StartCoroutine( PutText(x.CastToString()) );
  159.                 //m_Text.text = m_CurMsg;
  160.  
  161.                 Debug.Log(x);
  162.  
  163.                 yield return textPut;
  164.  
  165.                 switch (WaitKey)
  166.                 {
  167.                     case EZ_WAIT_KEY._NONE:
  168.                         yield return null;
  169.                         break;
  170.                     case EZ_WAIT_KEY.TIME:
  171.                         yield return new WaitForSeconds(3);
  172.                         break;
  173.                     case EZ_WAIT_KEY.KEY:
  174.                         yield return new WaitUntil(() => Input.anyKeyDown);
  175.                         break;
  176.                 }
  177.             }
  178.         }
  179.     }
  180.     //--------------------------------------------------
  181.     // メッセージを1文字ずつ表示するコルーチン
  182.     //--------------------------------------------------
  183.     IEnumerator PutText(string sText)
  184.     {
  185.         int iNum = 0;
  186.  
  187.         while (iNum < sText.Length)
  188.         {
  189.             m_CurMsg = sText.Substring(0, ++iNum);
  190.  
  191.             m_Text.text = m_CurMsg;
  192.  
  193.             yield return new WaitForSeconds(0.1f);
  194.         }
  195.     }
Advertisement
Add Comment
Please, Sign In to add comment