ユーザ用ツール

サイト用ツール


embed:tutorial2

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
embed:tutorial2 [2023/01/16 21:29] machiaworxembed:tutorial2 [2023/01/21 13:15] (現在) – [実装] machiaworx
行 31: 行 31:
 using Miniscript; using Miniscript;
 using TMPro; using TMPro;
- 
  
 public class ScriptMover : MonoBehaviour public class ScriptMover : MonoBehaviour
行 40: 行 39:
     public Interpreter interpreter;     public Interpreter interpreter;
  
-    public string globalVarName="";+    //ここにグローバル変数を格納するクラス名を指定してください。 
 +    public string globalVarName="ship";
     private int frame=0;     private int frame=0;
          
行 48: 行 48:
  
         interpreter.hostData = this;         interpreter.hostData = this;
-    //出力+
         //標準出力先の指定         //標準出力先の指定
         interpreter.standardOutput = (string s,bool t) => Debug.Log(s);         interpreter.standardOutput = (string s,bool t) => Debug.Log(s);
         interpreter.implicitOutput = (string s,bool t) => Debug.Log($"implicit {s}");         interpreter.implicitOutput = (string s,bool t) => Debug.Log($"implicit {s}");
         interpreter.errorOutput = (string s,bool t) => {         interpreter.errorOutput = (string s,bool t) => {
- +            //エラーログ出力。(Unity側のエラーとして動く)
-        //エラーログ出力。(Unity側のエラーとして動く)+
             Debug.LogError(s);             Debug.LogError(s);
         };         };
  
-        //関数定義+        //組込関数定義のための変数
         Intrinsic f;         Intrinsic f;
  
-        //ボスの座標一次保存、次のエリアで切替できるよう+        //文字列テキスト枠反映させ
         f = Intrinsic.Create("change");         f = Intrinsic.Create("change");
         f.AddParam("str", "");         f.AddParam("str", "");
行 70: 行 69:
         };         };
  
 +        //ソースコードの起動準備を行う。
         RunScript(loadText);         RunScript(loadText);
     }     }
          
     public void RunScript(string sourceCode) {     public void RunScript(string sourceCode) {
 +        //前提になるソースコードを最初から埋め込んでおく。
         string extraSource = "ship.reset = function(); self.x=0; self.y=0; self.rot=0;end function\n";         string extraSource = "ship.reset = function(); self.x=0; self.y=0; self.rot=0;end function\n";
 +        
 +        //上記前提ソースコード+テキストのソースコードを読み込ませる。
         interpreter.Reset(extraSource+sourceCode);         interpreter.Reset(extraSource+sourceCode);
 +        
 +        //コンパイル処理
         interpreter.Compile();         interpreter.Compile();
  
 +        //グローバル変数の初期化。
         ValMap data = new ValMap();         ValMap data = new ValMap();
-     
         data["cnt"]=new ValNumber(0);         data["cnt"]=new ValNumber(0);
         data["x"] = new ValNumber(this.transform.localRotation.x);         data["x"] = new ValNumber(this.transform.localRotation.x);
         data["y"] = new ValNumber(this.transform.localRotation.y);         data["y"] = new ValNumber(this.transform.localRotation.y);
         data["rot"] = new ValNumber(this.transform.localRotation.z);         data["rot"] = new ValNumber(this.transform.localRotation.z);
-    +         
 +        //グローバル変数の定義。今回は1個のマップにまとめてる
         interpreter.SetGlobalValue(globalVarName, data);         interpreter.SetGlobalValue(globalVarName, data);
     }     }
  
     void Update(){     void Update(){
 +        //コンパイルしたソースコードを実行する
         try{         try{
-            //interpreter.Restart(); 
             if( !interpreter.Running())             if( !interpreter.Running())
                 interpreter.Restart();                 interpreter.Restart();
             interpreter.RunUntilDone(1.0f);             interpreter.RunUntilDone(1.0f);
-  
         }catch (MiniscriptException err) {         }catch (MiniscriptException err) {
 #if UNITY_EDITOR #if UNITY_EDITOR
行 101: 行 105:
 #endif #endif
         }         }
- +         
 +        //処理したスクリプトのグローバル変数から各種動きを反映する
         UpdateFromScript();         UpdateFromScript();
     }     }
行 108: 行 113:
         ValMap data = null;         ValMap data = null;
         try {         try {
 +            //変数名が最初に定義したものかを確認、一致してれば以降読み込めてるものとして処理する。
             data = interpreter.GetGlobalValue(globalVarName) as ValMap;             data = interpreter.GetGlobalValue(globalVarName) as ValMap;
         } catch (UndefinedIdentifierException) {         } catch (UndefinedIdentifierException) {
行 114: 行 120:
         if (data == null) return;         if (data == null) return;
    
-        Transform t = transform; +        //CubeのRotationを変更する
-        Vector3 pos = t.localPosition; +
-        float xx,yy; +
- +
         Value xval = data["x"];         Value xval = data["x"];
         Value yval = data["y"];          Value yval = data["y"]; 
行 123: 行 126:
         if (rotVal != null) t.localRotation = Quaternion.Euler(xval.FloatValue(), yval.FloatValue(), (float)rotVal.FloatValue());         if (rotVal != null) t.localRotation = Quaternion.Euler(xval.FloatValue(), yval.FloatValue(), (float)rotVal.FloatValue());
  
 +        //全体フレーム数を加算する
         Value cntval=data["cnt"];         Value cntval=data["cnt"];
         frame=cntval.IntValue();         frame=cntval.IntValue();
行 130: 行 134:
 </code> </code>
  
-終わったら画面UIのテキストをCubeの枠にドラッグ&ドロップしておいてください。+終わったらCubeからテキスト部分参照しておいてください。
  
 ==== Miniscript言語で書いたソースコード用意 ==== ==== Miniscript言語で書いたソースコード用意 ====
  
-以下のテキストを用意して、ScriptMoverクラスのテキストに登録しておいてください。+以下の内容のテキストファイルを用意してください。
  
   serif=["This is Miniscript's test.","This is sample."]   serif=["This is Miniscript's test.","This is sample."]
行 141: 行 145:
   ship.cnt+=1   ship.cnt+=1
   change(serif[(ship.cnt/600)%2])   change(serif[(ship.cnt/600)%2])
 +  
 +終わったらCubeから作ったソースコードを参照しておいてください。
  
 ==== 動かしてみましょう ==== ==== 動かしてみましょう ====
embed/tutorial2.1673872140.txt.gz · 最終更新: 2023/01/16 21:29 by machiaworx