是否可以在 Eclipse 中访问 Unity 游戏的变量(例如高分)?
变量存储在 Unity 脚本中,如下所示:
using Assets.Scripts.Events;
using Assets.Scripts.Utils;
using UnityEngine;
namespace Assets.Scripts
{
public class ScoreManager : CommandMonoBehaviour
{
public int Score;
public void Start()
{
Subscribe(GameEvents.BasketGotMovingObject, OnBasketGotMovingObject);
}
private void OnBasketGotMovingObject(GameEventArgs<MovingObject> args)
{
Score = Mathf.Max(0, Score + args.Value.BonusPoints);
GameEvents.ScoreUpdated.Publish(new GameEventArgs<int>(Score));
}
}
}
该项目已通过 Unity IDE 导出到“Google Android 项目”。
导出的项目中的
onCreate
函数如下所示:
protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGB_565);
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
View playerView = mUnityPlayer.getView();
setContentView(playerView);
playerView.requestFocus();
}
只有一个“UnityPlayer”。 我如何访问像
int Score
这样的脚本变量?
假设您已在 Eclipse 中打开解决方案,请确保您获取了脚本所附加的游戏对象的组件。你可以用
var scoreManager = GameObject.FindOnbjectOfType(typeof(ScoreManager)) as ScoreManager;
如果您不知道ScoreManager脚本所属的对象。如果您确实知道 te 脚本所属的对象,您可以使用以下内容(将 obj 替换为实际名称)
var scoreManager = obj.GetComponent<ScoreManager>():
从那里你可以做
scoreManager.Score
我还假设您在某个时刻派生 ScoreManager 的类派生自 MonoBehaviour,它将脚本附加到场景中的游戏对象所需的类。
希望这有帮助