[英]Unity3D C#-Pass List value to another class
may I ask about how to pass List value to another class?For example : 我可以问一下如何将List值传递给另一个类吗?例如:
using System.Collections.Generic;
public class PassListClass : MonoBehaviour
{
List<int> passInt=new List<int>();
public void Start()
{
passInt.Add(1);
passInt.Add(3);
}
.
.
.
}
So I want to pass 1 and 3 to another class,what should I do after create class instance and call it at another class?I already tried to call the List value to another class but seen not working.Actually I want to pass a list of game object to another class,just trying to get some idea.Thanks for help. 所以我想将1和3传递给另一个类,在创建一个类实例并在另一个类中调用它该怎么办?我已经尝试过将List值传递给另一个类,但是却无法正常工作。实际上我想传递一个列表将游戏对象转移到另一个类,只是试图获得一些想法。谢谢您的帮助。
Why not assign it in the new class constructor? 为什么不在新的类构造函数中分配它?
Class1 -> Class2 1级-> 2级
You could do something like: 您可以执行以下操作:
Class2 newInstance = new Class2(your_list_from_Class1);
the constructor could be something like: 构造函数可能类似于:
public Class2(List<something> listToPass){
this.list = listToPass;
}
I'm not sure I understood your answer completely but I hope I helped. 我不确定我是否完全理解您的回答,但希望能有所帮助。 I'm gonna leave here a list of answers that might help you out:
我要在这里留下一些可能对您有帮助的答案:
If both classes are scripts (Extended by MonoBehaviour) then you can't pass a value from Class A into Class B Constructor. 如果两个类都是脚本(由MonoBehaviour扩展),则不能将值从A类传递给B类构造函数。
However, what you can do is to either 1. Set the variable you want to pass from Class A -> Class B into a static variable. 但是,您可以执行以下任一操作:1.将要从A类-> B类传递的变量设置为静态变量。 Then you can just use
那你就可以用
Within Class B B级以内
Variable localVariableName;
public void Update() {
if(localVariableName==null)
localVariableName = ClassA.VariableName;
}
// -- OR if you know that ClassA will run before ClassB --
public void Start() {
localVariableName = ClassA.VariableName;
}
-- OR -- If you dont want to use a static variable you can find the GameObject that has the ClassA as script and reference to that variable from within Class B -或-如果您不想使用静态变量,则可以找到具有ClassA作为脚本并从Class B中引用该变量的GameObject。
Variable localVariableName;
public GameObject OwnerOfClassA;
public void Start() {
localVariableName = OwnerOfClassA.GetComponent<ClassA>().VariableName;
}
In the Unity3D Editor, just drag the gameobject that owns classA into OwnerOfClassA 在Unity3D编辑器中,只需将拥有classA的游戏对象拖到OwnerOfClassA中
I hope this helps! 我希望这有帮助!
Cheers, Zerratar. 干杯,扎拉塔。
So this is actually what I do when passing List value to another class.Hope it help. 所以这实际上是我在将List值传递给另一个类时所做的事情。希望对您有所帮助。
public class ListClass : MonoBehaviour
{
List<int> intList=new List<int>();
private static ListClass instance;
public void Start()
{
intList.Add(1);
intList.Add(2);
lnstance=this
}
}
For another class 对于另一堂课
public class ValueFromListClass : MonoBehaviour
{
private ListClass listClass;
List<int> valueFromListClass=new List<int>();
public void Start()
{
listClass=ListClass.instance;
valueFromListClass=listClass.intList;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.