示例#1
0
        static void updateProperties(Transform dstRoot)
        {
            foreach (Component dstComponent in components)
            {
                var type = dstComponent.GetType();

                if (dstComponent == null)
                {
                    continue;
                }

                var so = new SerializedObject(dstComponent);
                so.Update();
                var iter = so.GetIterator();

                // Object Reference
                while (iter.NextVisible(true))
                {
                    if (iter.propertyType.ToString() != "ObjectReference")
                    {
                        continue;
                    }

                    SerializedProperty property = so.FindProperty(iter.propertyPath);
                    var dstObjectReference      = property.objectReferenceValue;
                    if (dstObjectReference == null)
                    {
                        continue;
                    }
                    if (!(dstObjectReference is Transform || dstObjectReference is Component))
                    {
                        continue;
                    }

                    Transform srcTransform = null;
                    if (dstObjectReference is Component)
                    {
                        srcTransform = (dstObjectReference as Component).transform;
                    }
                    else if (dstObjectReference is Transform)
                    {
                        srcTransform = dstObjectReference as Transform;
                    }

                    // ObjectReferenceの参照先がコピー内に存在するか
                    if (!transforms.Contains(srcTransform))
                    {
                        continue;
                    }

                    // コピー元のルートからObjectReferenceの位置への経路を探り、コピー後のツリーから該当オブジェクトを探す
                    var routes = SearchRoute(root, srcTransform);
                    if (routes == null)
                    {
                        continue;
                    }
                    Transform current = dstRoot;
                    foreach (var route in routes)
                    {
                        // 次の子を探す(TreeItemの名前と型で経路と同じ子を探す)
                        var children = current.GetComponentsInChildren <Transform>();
                        if (children.Length < 1)
                        {
                            current = null;
                            break;
                        }
                        Transform next = null;
                        foreach (Transform child in children)
                        {
                            var treeitem = new TreeItem(child.gameObject);
                            if (treeitem.name == route.name && treeitem.type == route.type)
                            {
                                next = child;
                                break;
                            }
                        }
                        if (next == null)
                        {
                            current = null;
                            break;
                        }

                        current = next;
                    }
                    if (current == null)
                    {
                        continue;
                    }

                    if (dstObjectReference is Transform)
                    {
                        property.objectReferenceValue = current;
                    }

                    if (dstObjectReference is Component)
                    {
                        Component comp     = null;
                        var       children = current.GetComponentsInChildren <Component>();
                        foreach (Component child in children)
                        {
                            if (child.GetType() == dstObjectReference.GetType())
                            {
                                comp = child;
                                break;
                            }
                        }
                        if (comp == null)
                        {
                            continue;
                        }
                        property.objectReferenceValue = comp;
                    }
                }
                so.ApplyModifiedProperties();
            }
        }