• Unity3D 里 C# 和JS互相访问方法


    方法01

    ------------------------JS访问C#的变量-----------------------------

    JS脚本:

    import System.Reflection;
    var ee = 0;
    var ddf:GameObject;
    function OnGUI ()
    {
     var targetController1:Component= ddf.GetComponent("ctrl");
     var fieldInfo: FieldInfo=targetController1.GetType().GetField("csvalue");

     if(GUI.Button(Rect(100,100,100,100),"Call"))
     {
      ee=fieldInfo.GetValue(targetController1);
      print(ee.ToString());
     }
    }

    C#脚本:

    using UnityEngine;
    using System.Collections;

    public class ctrl : MonoBehaviour
    {
        public int csvalue = 100;
     void Start ()
     {
     }
     
     void Update ()
     {
     
     }
    }

    -----------------------c#访问JS变量------------------------

    c#代码:

    using UnityEngine;
    using System.Collections;
    using System;
    using System.Reflection;

    public class CtoJS : MonoBehaviour {
     
     public GameObject tt;
     public String moneycount;
        public String boncount;
     // Use this for initialization
     void Start ()
     {
           Component AComponent = tt.GetComponent("CllisionsSomeThing");
         FieldInfo fieldInfo = AComponent.GetType().GetField("MC");
         FieldInfo fieldInfo1 = AComponent.GetType().GetField("BomCount");  
         moneycount = fieldInfo.GetValue(AComponent).ToString();
         boncount= fieldInfo1.GetValue(AComponent).ToString();
      
         print(moneycount);
         print(boncount);
     }
     
     void Update ()
     {
     }
    }

    js代码:

    #pragma strict

    var MC = "sdfsdfs";
    var BomCount = 1000;

    function Start () {

    }

    function Update () {

    }

    -------------------------------------------------------------------------------------------------

    方法02

    兩個文件 test1.js 和 test2.cs

    test1.js

    function OnGUI()
    {    
        
    if(GUI.Button(Rect(25,25,100,30),"JS Call CS"
     ))
        
    {
            
    var c = gameObject.GetComponent("test2"
    );
            c.PrintTest();
        }

    }


    function testPrint()
    {
        print(
    "CS Call JS"
    );
    }

     

    test2.cs

    using UnityEngine;
    using
     System.Collections;

    public class test2: MonoBehaviour 
    {

        
    void
     OnGUI()
        
    {
            
    if(GUI.Button(new Rect(25,70,100,30), "CS Call JS"
    ))
            
    {
                test1 c 
    = (test1)gameObject.GetComponent("test1"
    );
                c.testPrint();
            }

        }


        
    void PrintTest()
        
    {
            print(
    "JS Call CS"
    );
        }

    }

     

    這里必須要注意的是JS文件必須是在 "Standard Assets"、 "Pro Standard Assets" 和 "Plugins" 這三個目錄中的任何一個里,而CS文件不能與JS文件在一個目錄中。原因是,這三個目錄里的腳本被最先編譯,"Editor"目錄里的稍后編譯,其他的腳本最后編譯。目前Unity3D的2.5的版本似乎不支持C# 3.0,所以無法用var的關鍵字,這樣就只支持強類型,所以如果在一個目錄下則CS文件無法讀取JS里的方法,也就無法編譯通過了。而JS調用CS方法則無此限制。

    具體可參考 http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

  • 相关阅读:
    arcengine 文件夹连接
    [WinForm]DataGridView列头右键菜单
    Arcengine编辑代码
    map与pagelayout同步新方法
    清华教授李稻葵:恒昌、宜信过去三四年走过了西方国家20年的历程!
    delete
    股权融资与债务融资之区别 创业者一定要看懂
    《乌镇指数:全球人工智能发展报告2016》正式发布
    2017年美国名校录取中国大陆学生数据汇总
    IDG资本全球拼图:近10年揽26家独角兽,最敢出手VC再造"VC+"
  • 原文地址:https://www.cnblogs.com/softimagewht/p/2381762.html
Copyright © 2020-2023  润新知