简体   繁体   English

Unity3D C#-将列表值传递给另一个类

[英]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: 我要在这里留下一些可能对您有帮助的答案:

Passing value from one class to another 将价值从一类传递到另一类

Passing a list to another class 将清单传递给另一个班级

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.

相关问题 UNity3D,在Unity3D中将值从一种类/方法返回到另一种类/方法 - UNity3D, return value from one class/method to another class/method in Unity3D 在Unity3d中将值传递给一个脚本传递给另一个脚本 - Pass Value to One Script to Another in Unity3d 一个变量来保存这个类中的值到unity3d中另一个类中的值? - A variable to hold value in this class to from another class in unity3d? 检查器值无法从Unity3d中的另一个类访问 - Inspector value cannot access from another class in Unity3d 如何使用C#访问另一个类(在unity3d中)? - How to access another class using C#(in unity3d)? 如何在 Unity3d 中将 C# 组件从一个游戏对象传递到另一个游戏对象 - how can I pass a C# component from one gameobject to another in Unity3d 从另一个脚本操作C#时Unity3D静态布尔值未更新 - Unity3D static bool value not updating when manipulating from another script c# 如何使用 ConfigurationManager 将 Unity3d 中的连接字符串值传递到类库 - How to pass connectionstring value from Unity3d to a classlibrary with ConfigurationManager 如何将字符串参数传递给unity3d中的另一个脚本 - How to pass string argument to another script in unity3d C#/ Unity3d - 将对象类型作为参数传递 - C# / Unity3d - pass object type as parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM