Exemple #1
0
        private GameObject InstantiateChild(PrefabInitializeData initializeData)
        {
            GameObject instance = null;

                        #if UNITY_EDITOR
            if (!EditorApplication.isPlayingOrWillChangePlaymode)
            {
                instance = (GameObject)PrefabUtility.InstantiatePrefab(initializeData.assetPrefab);
            }
            if (instance == null)
                        #endif
            instance = (GameObject)Instantiate(initializeData.assetPrefab);
            instance.transform.SetParent(this.transform);
            instance.transform.localPosition = initializeData.localPosition;
            instance.transform.localScale    = initializeData.LocalScale;
            instance.transform.localRotation = Quaternion.Euler(initializeData.EulerAngleRotation);
            var initializeDataForRectTransform = initializeData as PrefabInitializeDataForRectTransform;
            if (initializeDataForRectTransform != null)
            {
                var rectTransform = instance.GetComponent <RectTransform> ();
                rectTransform.anchorMax = initializeDataForRectTransform.AnchorMax;
                rectTransform.anchorMin = initializeDataForRectTransform.AnchorMin;
                rectTransform.offsetMax = initializeDataForRectTransform.OffsetMax;
                rectTransform.offsetMin = initializeDataForRectTransform.OffsetMin;
                rectTransform.pivot     = initializeDataForRectTransform.Pivot;
                rectTransform.sizeDelta = initializeDataForRectTransform.SizeDelta;
            }
            return(instance);
        }
Exemple #2
0
        public void GenerateNestedPrefabData()
        {
            prefabInitializeDataList.Clear();
            prefabInitializeDataForRectTransformList.Clear();
            HojoLogger.Log("GenerateNestedPrefabData invoked " + gameObject.name);
            var        recognizedTransformList = gameObject.GetComponentsInChildren <Transform> ().Where(m => m.parent == this.transform).OrderByDescending(m => m.childCount).ToList();
            GameObject originalOfThisObject    = null;

            try {
                originalOfThisObject = ((Transform)PrefabUtility.GetPrefabParent(transform)).gameObject;
            } catch {
                return;
            }
            int index = 0;

            recognizedTransformList.ForEach(m => {
                GameObject original = null;
                try {
                    original = ((Transform)PrefabUtility.GetPrefabParent(m)).gameObject;
                } catch {
                    HojoLogger.LogWarning("Nested Prefab Contents need to be Unity Prefab.(Register to Project View as a persistent file.)", HojoLogger.LoggerColor.WarningOrange);
                    return;
                }
                if (original == originalOfThisObject)
                {
                    HojoLogger.LogWarning("You cannot nest same prefab.You need infinitely long something? Give me a break!", HojoLogger.LoggerColor.WarningOrange);
                    return;
                }

                var newInitializeData = new PrefabInitializeData();



                var rectTransform = m.gameObject.GetComponent <RectTransform> ();
                if (rectTransform != null)
                {
                    var newInitializeDataForRect                = new PrefabInitializeDataForRectTransform();
                    newInitializeDataForRect.assetPrefab        = original;
                    newInitializeDataForRect.localPosition      = m.localPosition;
                    newInitializeDataForRect.EulerAngleRotation = m.localRotation.eulerAngles;
                    newInitializeDataForRect.LocalScale         = m.localScale;
                    newInitializeDataForRect.AnchorMax          = rectTransform.anchorMax;
                    newInitializeDataForRect.AnchorMin          = rectTransform.anchorMin;
                    newInitializeDataForRect.OffsetMax          = rectTransform.offsetMax;
                    newInitializeDataForRect.OffsetMin          = rectTransform.offsetMin;
                    newInitializeDataForRect.Pivot              = rectTransform.pivot;
                    newInitializeDataForRect.SizeDelta          = rectTransform.sizeDelta;
                    newInitializeDataForRect.Index              = index++;
                    var contextList = m.GetComponents <INestedPrefabRootSerializingContextBridge> ();
                    for (int i = 0; i < contextList.Length; i++)
                    {
                        newInitializeDataForRect.injectionContextList.Add(contextList [i].GetExpectedDataAsContext());
                    }
                    prefabInitializeDataForRectTransformList.Add(newInitializeDataForRect);
                }
                else
                {
                    newInitializeData.assetPrefab        = original;
                    newInitializeData.localPosition      = m.localPosition;
                    newInitializeData.EulerAngleRotation = m.localRotation.eulerAngles;
                    newInitializeData.LocalScale         = m.localScale;
                    newInitializeData.Index = index++;
                    var contextList         = m.GetComponents <INestedPrefabRootSerializingContextBridge> ();
                    for (int i = 0; i < contextList.Length; i++)
                    {
                        newInitializeData.injectionContextList.Add(contextList [i].GetExpectedDataAsContext());
                    }
                    prefabInitializeDataList.Add(newInitializeData);
                }
            });


            isInitialized = false;
        }