【问题标题】:Is that Possible To Add List<item> value from different Script File Unity C#是否可以从不同的脚本文件 Unity C# 添加 List<item> 值
【发布时间】:2016-06-30 15:06:33
【问题描述】:

我有一个简单的问题。

我有两个文件脚本:

  1. 播放器.cs

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class player {
        public List<item> itemx = new List<item> (); // Here
    
    
    }
    
  2. 文件 Inventory.cs

            using UnityEngine;
            using System.Collections;
            using System.Collections.Generic;
            using UnityEngine.UI;
    
            public class inventory : MonoBehaviour {
                public player playerx;
    
                itemDatabase database;
    
                // Use this for initialization
                void Start () {
    
                    database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();
                    //Generate the Slot and Slot Name;
                    for(int i = 1; i <= 24; i++) {
                        playerx.itemx.Add(new item()); // Here
    
                        playerx.itemx[i] = database.items[i]; // And Here
    
                    }
                }
            }
    

正如你在 player.cs 文件中看到的,我声明了一个列表变量:

public List<item> itemx = new List<item> (); // Here

在 Inventory.cs 文件中,我想使用以下方法添加值:

playerx.itemx.Add(new item()); And Here

是否可以将 value 变量保存到 player.cs 文件中?

谢谢

【问题讨论】:

  • 你需要类的实例。

标签: c# list class unity3d unityscript


【解决方案1】:

注意

在你的问题中,你有

   public class Player

而不是

   public class Player:MonoBehaviour

我认为这只是一个错字。如果您想拥有一个“免费”类 Player(如在 OO 环境中),则不能这样做。


正确,这样做绝对有问题!

有几件事...

请注意,在第二个脚本中它将是:

 public player playerx; ... WRONG

 public Player player; ... CORRECT

然后只是

player.items.Add( .. etc )

(别忘了,你当然必须拖动来连接“Player player”检查器变量。如果你不知道怎么做,可以这样说,我会给你一个教程链接。)

其次,在第一个脚本中你有问题。

Unity 中有一个愚蠢的地方,“public”的意思是“inspector variable”

事实上,你想要一个“普通”的公共变量,就像在任何普通编程语言中一样,奇怪的是你必须输入这个

“[System.NonSerialized] 公共”

这只是关于 Unity 的那些奇怪的事情之一。事实上,在许多项目中,您从不使用检查器变量。因此,您只需在任何地方不断输入“[System.NonSerialized] public”。如果您明白我的意思,那么长的“[System.NonSerialized] public”是一种“正常”的。一直使用它。仅当您特别需要检查器变量时,才使用“public”。

  public List<item> itemx...   WRONG

  [System.NonSerialized] public List<item> items...   CORRECT

【讨论】:

  • 或使用属性。但重要的是:他需要对玩家的引用。因为它不是从 MonoBehaviour 派生的,所以他需要 player = new Player() 在某个地方,并且在构造函数中初始化列表也可能是个好主意。
  • 啊!我只是假设它是一种 MonoBehaviour。回想一下,如果它不是 Unity 组件,他甚至不能使用检查器变量或其他任何东西。
【解决方案2】:

这个怎么样。假设数据库对象返回 [items] 什么的。

                database = GameObject.FindGameObjectWithTag 

("itemDatabase").GetComponent<itemDatabase> ();
                    //Generate the Slot and Slot Name;
                    forach(item _item in database.items)
    {
 playerx.itemx.add(_item);
}

如果您的数据库返回不同的内容,您可以尝试

playerx.itemx.add(new item
{
itemproperty1 = _item.property1, 
and so on.
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2014-09-22
    • 2014-06-26
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多