C# 尝试使用不同脚本中的函数需要静态更新,但会破坏代码的统一性
我尝试在win.cs中使用不同脚本上的许多函数 比如说C# 尝试使用不同脚本中的函数需要静态更新,但会破坏代码的统一性,c#,function,unity3d,C#,Function,Unity3d,我尝试在win.cs中使用不同脚本上的许多函数 比如说 team.cs PlayGame(); 我将一些变量设置为static,但不是所有变量,因为不需要(还没有?) 我试着把PlayGame(游戏)放进去;和UpdateScore();在win.cs上。这需要我做一个: public static void UpdateScore(){} 现在这是静态的,我必须去让所有与UpdateScore相关的其他变量都是静态的,然后代码就中断了 有没有更好的方法来做我正在做的事情 我尝试过使用
team.cs
PlayGame();
我将一些变量设置为static,但不是所有变量,因为不需要(还没有?)
我试着把PlayGame(游戏)放进去;和UpdateScore();在win.cs上。这需要我做一个:
public static void UpdateScore(){}
现在这是静态的,我必须去让所有与UpdateScore相关的其他变量都是静态的,然后代码就中断了
有没有更好的方法来做我正在做的事情
我尝试过使用team.PlayGame()作为变量,但这也需要整个静态过程
对不起,这很难解释
注意:所有函数都是公共函数。您只需使用从另一个脚本调用脚本的方法即可
来自win.cs
脚本的类似内容:
team teamScript = GameObject.Find("Object that team script is on that").GetComponent<team>();
teamScript.SendMessage("PlayGame", you parameter);
team teamScript=GameObject.Find(“团队脚本所在的对象”).GetComponent();
teamScript.SendMessage(“PlayGame”,您的参数);
如果Win.cs需要在Score.cs上调用UpdateScore,那么您可以这样做:
public class Win : MonoBehaviour{
[SerializeField]private Score score = null;
private void Start(){
if(this.score == null){
this.score = this.gameObject.GetComponent<Score>();
}
}
// Then you can use this.score.UpdateScore when needed
}
public-class-Win:monobhavior{
[SerializeField]私有分数=空;
私有void Start(){
如果(this.score==null){
this.score=this.gameObject.GetComponent();
}
}
//然后,您可以在需要时使用this.score.UpdateScore
}
可以使用事件以另一种方式执行相同的操作
public class Win:MonoBehaviour{
public EventHandler<System.EventArg> RaiseUpdateScore;
protected void OnUpdateScore(EventArg args){
if(RaiseUpdateScore != null){
RaiseUpdateScore(this, args);
}
}
public void SomeAction(){
if(someCondition){
OnUpdateScore(null);
}
}
void OnDestroy(){ RaiseUpdateScore = null; }
}
public-class-Win:monobhavior{
公共事件处理程序RaiseUpdateScore;
受保护的void OnUpdateScore(EventArg args){
if(RaiseUpdateScore!=null){
RaiseUpdateScore(此参数为args);
}
}
公共行动{
如果(某些条件){
OnUpdateScore(空);
}
}
void OnDestroy(){RaiseUpdateScore=null;}
}
然后在Score.cs上
public class Score:MonoBehaviour{
private Win win = null;
private void Start(){
this.win = this.gameObject.GetComponent<Win>();
this.win.RaiseUpdateScore += HandleUpdateScore;
}
private void HandleUpdateScore(object sender , System.EventArg args){}
private void OnDestroy(){
if(this.win != null){
this.win.RaiseUpdateScore -= HandleUpdateScore;
}
}
}
公共课堂分数:单一行为{
私人赢赢=空;
私有void Start(){
this.win=this.gameObject.GetComponent();
this.win.RaiseUpdateScore+=HandleUpdateScore;
}
私有void HandleUpdateScore(对象发送方,System.EventArg args){}
私有void OnDestroy(){
如果(this.win!=null){
this.win.RaiseUpdateScore-=HandleUpdateScore;
}
}
}
创建团队T代码>分数S代码>并在开始函数中初始化S=gameobject.GetComponent()
T=gameobject.GetComponent()在win.c中编码>并通过T和S访问,如T.xxxx()代码>S.xxxxxx()代码>很多选项可以在所有链接上执行此检查ok我将尝试此功能谢谢!
public class Score:MonoBehaviour{
private Win win = null;
private void Start(){
this.win = this.gameObject.GetComponent<Win>();
this.win.RaiseUpdateScore += HandleUpdateScore;
}
private void HandleUpdateScore(object sender , System.EventArg args){}
private void OnDestroy(){
if(this.win != null){
this.win.RaiseUpdateScore -= HandleUpdateScore;
}
}
}