这是我在这里的第一篇文章,所以如果读起来有点困惑的话,很抱歉。我也有阅读障碍,所以请原谅我的写作。
所以我试着做一个自上而下的rpg。我已经创建了一个结构来保存武器,并使用了一个数组,这样我就可以在检查器中填充它。
我希望能够做的是从一个清单或其他东西中传入id变量,然后访问其他变量,如伤害和游戏对象。
using UnityEngine;
using System.Collections;
[System.Serializable]
public struct Weapons {
public string ID;
public GameObject WeaponFront;
public GameObject WeaponBack;
public GameObject WeaponLeft;
public GameObject WeaponRight;
public float Damage;
}
public class WeaponHandler : MonoBehaviour {
public Weapons[] Weapon;
}
所以我想像这样访问它
hitobj.GetComponent<PhotonView>().RPC ("TakeDamage",PhotonTargets.All, /* damage from struct here */);
做这件事最好的方法是什么?我不确定我到底在做什么,因为这是我第一次使用structs。
玩家在孩子的时候有4个游戏对象,这些是玩家面对不同方向的不同状态,每个对象下面都有武器图形,所以在结构中需要4个游戏对象,所以我可以激活正确的图形。
我在考虑使用枚举来切换使用什么武器,我可以创建一个枚举来容纳所有不同的武器,这样当我使用武器时,我可以访问枚举并将状态添加到该武器,并根据所选择的状态来选择武器处理程序中的相应id。
这就是我不确定该怎么做的,我不知道如何访问武器搬运工的id
发布于 2015-03-06 20:25:20
你的问题实际上更多的是关于单位而不是结构,你的结构就像一个类,所以你需要一个武器类型的实际武器列表,让你的命中对象访问它被击中的武器的伤害。
在没有看到所有代码的情况下,真的不能说哪里有武器id吗?您可以拥有所有武器的全局列表,然后使用linq查找您想要的武器
int id = hitobj.weaponId
float damage = globalWeapons.First(w=>w.id == id).Damage
发布于 2015-03-06 20:46:49
根据我对你的问题的理解,我想用一个字典来保存武器结构的Ids。
下面是一个插图:
Dictionary<String, Weapons> weaponsDico = new Dictionary<string, Weapons>();
// Load the weapons for the given ID
Weapons rambosArsenal = new Weapons();
rambosArsenal.ID = "RAMBO";
rambosArsenal.WeaponBack = new GameObject();
// ....
weaponsDico.Add("RAMBO", rambosArsenal);
Weapons terminatorArsenal = new Weapons();
terminatorArsenal.ID = "TERMINATOR";
terminatorArsenal.WeaponFront = new GameObject();
weaponsDico.Add(terminatorArsenal.ID, terminatorArsenal);
String targetID = ""; // Put here an ID from which you want to get the weapons
// In your other code
Weapons targetWeapons = new Weapons();
Boolean existsTargetWeapons = weaponsDico.TryGetValue(targetID, out targetWeapons);
if (existsTargetWeapons)
{
hitobj.GetComponent<PhotonView>().RPC ("TakeDamage",PhotonTargets.All, targetWeapons.Damage);
}
除了使用TryGetValue,您还可以使用带有lambda表达式的扩展方法
https://stackoverflow.com/questions/28898477
复制相似问题