Noirand

cs script test 03

May 12th, 2016
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.08 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.         public void AnsWait()
  41.         {
  42.             if (m_Parent != null)
  43.                 m_Parent.SetAnsWait();
  44.         }
  45.         public void ActWait()
  46.         {
  47.             if (m_Parent != null)
  48.                 m_Parent.SetActWait();
  49.         }
  50.         public void AddAsk(string sAsk)
  51.         {
  52.             if (m_Parent != null)
  53.                 m_Parent.AddAsk(sAsk);
  54.         }
  55.         public int GetAnswer()
  56.         {
  57.             if (m_Parent != null)
  58.                 return m_Parent.Answer;
  59.             return -1;
  60.         }
  61.     }
  62.  
  63.     public enum EZ_WAIT_KEY
  64.     {
  65.         _NONE   // 待たない
  66.         ,TIME   // 時間待ち
  67.         ,KEY    // キー入力待ち
  68.         ,ANSWER // 返答入力待ち
  69.         ,ACTION // アクション待ち
  70.     }
  71.  
  72.     public EZ_WAIT_KEY  WaitKey     { get; private set; }
  73.     public int  Answer          { get; private set; }
  74.     public void SetNoWait()     { WaitKey = EZ_WAIT_KEY._NONE; }
  75.     public void SetTimeWait()   { WaitKey = EZ_WAIT_KEY.TIME; }
  76.     public void SetKeyWait()    { WaitKey = EZ_WAIT_KEY.KEY; }
  77.     public void SetAnsWait()    { WaitKey = EZ_WAIT_KEY.ANSWER; }
  78.     public void SetActWait()    { WaitKey = EZ_WAIT_KEY.ACTION; }
  79.  
  80.     //--------------------------------------------------
  81.     // private
  82.     //--------------------------------------------------
  83.     private GameObject      m_PrefAskPanel;
  84.     private List<Button>    m_AskList;
  85.  
  86.     private Text    m_Text;
  87.     private Text    m_Name;
  88.     private string  m_CurMsg;
  89.  
  90.     // lua 用クラス
  91.     private Script      m_Lua;
  92.     private DynValue    m_Func;
  93.     private DynValue    m_LuaCoroutine;
  94.  
  95. //============================================================
  96. // 関数定義
  97. //============================================================
  98.     //--------------------------------------------------
  99.     // コンストラクタ
  100.     //--------------------------------------------------
  101.     void Awake()
  102.     {
  103.         m_PrefAskPanel  = Resources.Load<GameObject>("Prefabs/Pref_AskPanel");
  104.         m_AskList       = new List<Button>();
  105.         Answer      = -1;
  106.  
  107.         UserData.RegisterAssembly();
  108.  
  109.         m_Text  = GetComponent<Text>();
  110.         m_Name  = transform.FindChild("Name").GetComponent<Text>();
  111.         m_Lua   = new Script();
  112.  
  113.         WaitKey = EZ_WAIT_KEY._NONE;
  114.  
  115. #if false
  116.         Dictionary<string, string> scripts = new  Dictionary<string, string>();
  117.  
  118.         object[] result = Resources.LoadAll("Lua", typeof(TextAsset));
  119.  
  120.         foreach(TextAsset ta in result.OfType<TextAsset>())
  121.         {
  122.             scripts.Add(ta.name, ta.text);
  123.         }
  124.  
  125.         Script.DefaultOptions.ScriptLoader = new MoonSharp.Interpreter.Loaders.UnityAssetsScriptLoader(scripts);
  126. #endif
  127.     }
  128.     //--------------------------------------------------
  129.     // 最初の更新
  130.     //--------------------------------------------------
  131.     void Start()
  132.     {
  133.         ((ScriptLoaderBase)m_Lua.Options.ScriptLoader).ModulePaths = //ScriptLoaderBase.UnpackStringPaths(Application.dataPath + "/Resources");
  134.             ScriptLoaderBase.UnpackStringPaths(UnityAssetsScriptLoader.DEFAULT_PATH + "/?");
  135.  
  136.         m_Lua.Globals["obj"] = new MyLua(this);
  137.  
  138.         var ret = m_Lua.DoFile("test");//Script.RunFile("test");
  139.         Debug.Log( ret.Table );
  140.         if (ret.Table != null)
  141.         {
  142.             foreach (DynValue pKey in ret.Table.Keys)
  143.             {
  144.                 Debug.Log(pKey + "," + ret.Table.Get(pKey));
  145.                 m_Text.text += ret.Table.Get(pKey) + "\n";
  146.             }
  147.  
  148.             //Debug.Log(ret.Table.Values);
  149.             //Debug.Log( m_Lua.Globals.Get("req").String );
  150.         }
  151.  
  152.         TryLuaCoroutine();
  153.     }
  154.     //--------------------------------------------------
  155.     // 更新処理
  156.     //--------------------------------------------------
  157.     void Update()
  158.     {
  159.    
  160.     }
  161.     //---------------------------------------------------
  162.     // Lua のコルーチンてすと
  163.     //---------------------------------------------------
  164.     void TryLuaCoroutine()
  165.     {
  166.         m_Func = m_Lua.DoFile("adv");
  167.         m_LuaCoroutine = m_Lua.CreateCoroutine(m_Func);
  168.         /*
  169.         foreach (DynValue x in m_LuaCoroutine.Coroutine.AsTypedEnumerable())
  170.         {
  171.             //DynValue x = coroutine.Coroutine.Resume();
  172.             m_Text.text += x + "\n";
  173.             Debug.Log(x);
  174.         }
  175. */     
  176.         StartCoroutine(ShowDialog());
  177.     }
  178.     //---------------------------------------------------
  179.     // Lua のコルーチンを受け取る C# コルーチン
  180.     //---------------------------------------------------
  181.     IEnumerator ShowDialog()
  182.     {
  183.         foreach (DynValue x in m_LuaCoroutine.Coroutine.AsTypedEnumerable())
  184.         {
  185.             //DynValue x = coroutine.Coroutine.Resume();
  186.             if (x.IsNotVoid())
  187.             {
  188.                 //m_Text.text = x.CastToString();
  189.                 m_CurMsg = "";
  190.  
  191.                 //var textPut = StartCoroutine( PutText(x.CastToString()) );
  192.                 var textPut = StartCoroutine( PutText(m_Lua.Globals["g_TalkWords"].ToString()) );
  193.                 m_Name.text = m_Lua.Globals["g_TalkChara"].ToString();
  194.                 //m_Text.text = m_CurMsg;
  195.  
  196.                 Debug.Log(x);
  197.  
  198.                 yield return textPut;
  199.  
  200.                 switch (WaitKey)
  201.                 {
  202.                     case EZ_WAIT_KEY._NONE:
  203.                         yield return null;
  204.                         break;
  205.                     case EZ_WAIT_KEY.TIME:
  206.                         yield return new WaitForSeconds(3);
  207.                         break;
  208.                     case EZ_WAIT_KEY.KEY:
  209.                         yield return new WaitUntil( () => Input.anyKeyDown );
  210.                         break;
  211.                     case EZ_WAIT_KEY.ANSWER:
  212.                         if (m_AskList.Count > 0)
  213.                         {
  214.                             yield return new WaitUntil( () => (Answer != -1) );
  215.                             Answer = -1;
  216.                             for (int ii=0; ii<m_AskList.Count; ++ii)
  217.                             {
  218.                                 Destroy(m_AskList[ii].gameObject);
  219.                             }
  220.                             m_AskList.Clear();
  221.                         }
  222.                         else {
  223.                             goto case EZ_WAIT_KEY.KEY;
  224.                         }
  225.                         break;
  226.                 }
  227.             }
  228.         }
  229.     }
  230.     //--------------------------------------------------
  231.     // メッセージを1文字ずつ表示するコルーチン
  232.     //--------------------------------------------------
  233.     IEnumerator PutText(string sText)
  234.     {
  235.         int iNum = 0;
  236.  
  237.         while (iNum < sText.Length)
  238.         {
  239.             m_CurMsg = sText.Substring(0, ++iNum);
  240.  
  241.             m_Text.text = m_CurMsg;
  242.  
  243.             yield return new WaitForSeconds(0.1f);
  244.         }
  245.     }
  246.     //--------------------------------------------------
  247.     // 質問追加
  248.     //--------------------------------------------------
  249.     public void AddAsk(string sAsk)
  250.     {
  251.         if (m_PrefAskPanel != null && m_AskList != null)
  252.         {
  253.             GameObject pObj = Instantiate(m_PrefAskPanel) as GameObject;
  254.             pObj.transform.SetParent(transform, false);
  255.             pObj.GetComponentInChildren<Text>().text = sAsk;
  256.  
  257.             Button btn = pObj.GetComponent<Button>();
  258.             btn.onClick.AddListener( () => {
  259.                 Answer = m_AskList.Count;
  260.                 Debug.Log("(>_<)< Answer is: " + sAsk);
  261.             });
  262.             m_AskList.Add(btn);
  263.         }
  264.     }
  265.     //--------------------------------------------------
  266. //============================================================
  267. }
Advertisement
Add Comment
Please, Sign In to add comment