【问题标题】:Access Static C# Variable from Javascript in Unity在 Unity 中从 Javascript 访问静态 C# 变量
【发布时间】:2016-05-23 16:15:06
【问题描述】:

我正在尝试在统一 3D 中从 javascript 访问 C# 静态变量。我如何实现它?我想得到分数让我在 javascript 中使用它。

Score.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Score : MonoBehaviour {
    public Text txt;
    public static int score;
    public GameObject gameobj;

    void Start () {
        txt = gameobj.GetComponent<Text>();
        txt.text = "Score : " + score;
    }


    void Update () {
        txt = gameobj.GetComponent<Text> ();
        txt.text = "Score : " + score;
        score = score + 1;
    }
    int GetScore(){
        return score;
    }
}

Collide.js

#pragma strict

 function OnTriggerEnter(otherObj: Collider){

    var scoreScript : Score;
        scoreScript  = GetComponent("Score");
        int score = scoreScript.GetScore();

         if (otherObj.tag == "Wall"){ 
            Debug.Log(score);
             Application.LoadLevel(2);
         }
          else if (otherObj.tag == "End"){ 
             Application.LoadLevel(6);
         }

 }

【问题讨论】:

  • 帮自己一个忙,不要混用语言。选择 C# 和 Score.score。

标签: javascript c# variables unity3d static


【解决方案1】:

让我用分数告诉你答案:

  1. C# 代码在 JS 代码之前编译
  2. 一般来说,JS代码可以访问C#类,反之则不行。
  3. 但是,您可以通过将脚本移动到较早编译的special folders 来影响编译顺序。您可以将您的 JS 脚本移动到一个名为“Plugins”的文件夹中,然后它就可以工作了。

您也可以查看(1) 以获得更多说明,同时在此处查看所有可用步骤的详细指南(2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2012-06-29
    • 2018-05-22
    相关资源
    最近更新 更多