是否可以从eclipse中的统一游戏中访问变量(例如高分)? 变量存储在统一脚本中,如下所示:
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 Project”。
导出项目中的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”?
答案 0 :(得分:1)
假设您已在eclipse中打开解决方案,请确保获取脚本附加到的游戏对象的组件。你可以用
var scoreManager = GameObject.FindOnbjectOfType(typeof(ScoreManager)) as ScoreManager;
如果您不知道ScoreManager脚本所属的对象。如果您确实知道te脚本所属的对象,则可以使用以下内容(将obj替换为实际名称)
var scoreManager = obj.GetComponent<ScoreManager>():
从那里你可以做到
scoreManager.Score
我还假设您从某个时刻派生ScoreManager的课程来自MonoBehaviour,这是将脚本附加到场景中游戏对象所需的类。
希望这有帮助