Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity3d。为复制的游戏对象获取相同的组件_C#_Unity3d - Fatal编程技术网

C# Unity3d。为复制的游戏对象获取相同的组件

C# Unity3d。为复制的游戏对象获取相同的组件,c#,unity3d,C#,Unity3d,我有一个游戏对象(myObject)和一个附加在该游戏对象某处的组件(myComponent)。 我复制游戏对象: var duplicate = Instantiate(myObject); 然后我想在我的replicategameObject上引用相同的组件 有没有办法在复制的对象上获得相同的组件? 我试图通过索引获取组件,但这不适用于游戏对象的层次结构。尝试以下方法: var duplicate = Instantiate(myObject) as GameObject; var

我有一个游戏对象(
myObject
)和一个附加在该游戏对象某处的组件(
myComponent
)。

我复制游戏对象:

var duplicate = Instantiate(myObject);
然后我想在我的
replicate
gameObject上引用相同的组件

有没有办法在复制的对象上获得相同的组件?
我试图通过索引获取组件,但这不适用于游戏对象的层次结构。

尝试以下方法:

var duplicate = Instantiate(myObject) as GameObject;
var componentDup = duplicate.GetComponent<__YOUR_COMPONENT__>();
var duplicate=实例化(myObject)为游戏对象;
var componentDup=duplicate.GetComponent();

您可以复制组件,但由于组件只能有一个父对象(游戏对象),这意味着一个组件实例不能与两个游戏对象共享

否则,如果您同意在两个游戏对象上都有两个单独的组件实例,您可以使用gameObject(带组件)创建Prefable并实例化Prefable

考虑到您可能有多个相同类型但具有不同设置(属性)的组件,并且您希望查找具有相同设置的组件,因此必须使用并循环结果以查找具有完全相同设置的新(复制)组件

考虑到(为简单起见)您查找名为Id的属性:

MyComponent myObjectsComponent = ... // here logic to find it, etc
GameObject duplicate = Instantiate(myObject);
List<MyComponent> myComponents = duplicate.GetComponents<MyComponent>();

// This can be replaced with bellow LINQ
MyComponent foundComponent = null;
foreach(MyComponent c in myComponents) {
  if (c.Id=myObjectsComponent.Id) {
    foundComponent = c;
    break;
  }
}
MyComponent myObjectsComponent=…//这里有找到它的逻辑等
游戏对象复制=实例化(myObject);
List myComponents=duplicate.GetComponents();
//这可以用bellow LINQ代替
MyComponent foundComponent=null;
foreach(myComponents中的MyComponent c){
if(c.Id=myObjectsComponent.Id){
foundComponent=c;
打破
}
}
或者,您可以使用LINQ简化循环:

MyComponent foundComponent = (from c in myComponents where c.Id=myObjectsComponent.Id select c).FirstDefault<MyComponent>();
MyComponent foundComponent=(从myComponents中的c开始,其中c.Id=myObjectsComponent.Id选择c);

好吧,我也要试着去理解你想要什么。。。我的解释是,您希望在
duplicate对象上引用特定的
myComponent
。很好,你可以这样做:

public class MyObject : MonoBehaviour {

    // Create a reference variable for the duplicate to have
    public Component theComponent { get; set; }

    void Start()
    {
        // Save the component you want the duplicate to have
        theComponent = GetComponent<Anything>(); 

        // Create the duplicate
        var duplicate = Instantiate(gameObject);

        // Set the component reference to the saved component
        duplicate.GetComponent<MyObject>().theComponent = theComponent;
    }

}
谢谢大家

我需要这样的东西:

public static T GetSameComponentForDuplicate<T>(T c, GameObject original, GameObject duplicate)
    where T : Component
{
    // remember hierarchy
    Stack<int> path = new Stack<int>();

    var g = c.gameObject;
    while (!object.ReferenceEquals(g, original))
    {
        path.Push(g.transform.GetSiblingIndex());
        g = g.transform.parent.gameObject;
    }

    // repeat hierarchy on duplicated object
    GameObject sameGO = duplicate;
    while (path.Count != 0)
    {
        sameGO = sameGO.transform.GetChild(path.Pop()).gameObject;
    }

    // get component index
    var cc = c.gameObject.GetComponents<T>();
    int componentIndex = -1;
    for (int i = 0; i < cc.Length; i++)
    {
        if (object.ReferenceEquals(c, cc[i]))
        {
            componentIndex = i;
            break;
        }
    }

    // return component with the same index on same gameObject
    return sameGO.GetComponents<T>()[componentIndex];
}
public static T GetSameComponentForDuplicate(tc、游戏对象原件、游戏对象副本)
其中T:组件
{
//记住等级制度
堆栈路径=新堆栈();
var g=c.gameObject;
而(!object.ReferenceEquals(g,original))
{
Push(g.transform.GetSiblingIndex());
g=g.transform.parent.gameObject;
}
//在重复的对象上重复层次结构
游戏对象sameGO=重复;
while(path.Count!=0)
{
sameGO=sameGO.transform.GetChild(path.Pop()).gameObject;
}
//获取组件索引
var cc=c.gameObject.GetComponents();
int componentIndex=-1;
对于(int i=0;i
此代码可以返回相同类型的不同组件。但我需要相同的组件哦,对不起,我认为实例化方法将创建另一个类和实例到新的gameobject中,所以。。。。它总是不同的。1个组件==1个游戏对象-是可以的。但假设我有困难的计算,以找到我需要的组件。然后复制根游戏对象。如何在复制的游戏对象上找到相同的(复制的)组件。使用复杂规则查找组件实例,2。当您找到组件实例时,您复制了它的游戏对象3。你想在新创建的游戏对象上访问相同的组件(类型)?是的,但肯定不是相同的类型,因为我可以有许多相同类型的组件。也许是索引或者别的什么
public static T GetSameComponentForDuplicate<T>(T c, GameObject original, GameObject duplicate)
    where T : Component
{
    // remember hierarchy
    Stack<int> path = new Stack<int>();

    var g = c.gameObject;
    while (!object.ReferenceEquals(g, original))
    {
        path.Push(g.transform.GetSiblingIndex());
        g = g.transform.parent.gameObject;
    }

    // repeat hierarchy on duplicated object
    GameObject sameGO = duplicate;
    while (path.Count != 0)
    {
        sameGO = sameGO.transform.GetChild(path.Pop()).gameObject;
    }

    // get component index
    var cc = c.gameObject.GetComponents<T>();
    int componentIndex = -1;
    for (int i = 0; i < cc.Length; i++)
    {
        if (object.ReferenceEquals(c, cc[i]))
        {
            componentIndex = i;
            break;
        }
    }

    // return component with the same index on same gameObject
    return sameGO.GetComponents<T>()[componentIndex];
}