public static Object ReadAGXFile(IO.AGXFileInfo info)
        {
            if (info == null || !info.IsValid)
            {
                return(null);
            }

            try {
                Object prefab = null;
                using (var inputFile = new IO.InputAGXFile(info)) {
                    inputFile.TryLoad();
                    inputFile.TryParse();
                    inputFile.TryGenerate();
                    prefab = inputFile.TryCreatePrefab();
                }

                // Updating scene instances with e.g., shape visual size etc.
                if (prefab != null)
                {
                    var restoredFileInstances = Object.FindObjectsOfType <AGXUnity.IO.RestoredAGXFile>();
                    foreach (var restoredFileInstance in restoredFileInstances)
                    {
#if UNITY_2018_3_OR_NEWER
                        var isReadPrefabInstance = PrefabUtility.GetPrefabInstanceStatus(restoredFileInstance.gameObject) == PrefabInstanceStatus.Connected &&
                                                   PrefabUtility.GetCorrespondingObjectFromSource(restoredFileInstance.gameObject) == prefab;
#else
                        var isReadPrefabInstance = PrefabUtility.GetPrefabType(restoredFileInstance.gameObject) == PrefabType.PrefabInstance &&
                                                   PrefabUtility.GetCorrespondingObjectFromSource(restoredFileInstance.gameObject) == prefab;
#endif
                        if (!isReadPrefabInstance)
                        {
                            continue;
                        }

                        var shapes = restoredFileInstance.GetComponentsInChildren <AGXUnity.Collide.Shape>();
                        foreach (var shape in shapes)
                        {
                            var visual = AGXUnity.Rendering.ShapeVisual.Find(shape);
                            if (visual != null)
                            {
                                visual.OnSizeUpdated();
                            }
                        }
                    }
                }

                return(prefab);
            }
            catch (System.Exception e) {
                Debug.LogException(e);
            }

            return(null);
        }
예제 #2
0
        public static UnityEngine.Object ReadAGXFile(IO.AGXFileInfo info)
        {
            if (info == null || !info.IsValid)
            {
                return(null);
            }

            try {
                UnityEngine.Object prefab = null;
                using (var inputFile = new IO.InputAGXFile(info)) {
                    inputFile.TryLoad();
                    inputFile.TryParse();
                    inputFile.TryGenerate();
                    prefab = inputFile.TryCreatePrefab();
                }

                return(prefab);
            }
            catch (System.Exception e) {
                Debug.LogException(e);
            }

            return(null);
        }
예제 #3
0
        public static GameObject Import(string file)
        {
            GameObject prefab        = null;
            bool       renameRoots   = false;
            var        dataDirectory = string.Empty;

            try {
                int iteration = 0;
                while (++iteration < 4)
                {
                    var fileInfo = new AGXFileInfo(file);
                    using (var inputFile = new InputAGXFile(fileInfo)) {
                        inputFile.TryLoad();
                        inputFile.TryParse();
                        var statistics = inputFile.TryGenerate();
                        renameRoots   = renameRoots || (iteration == 1 && statistics.RootsAddedToExistingAssets);
                        prefab        = inputFile.TryCreatePrefab();
                        dataDirectory = fileInfo.DataDirectory;

                        if (!statistics.HasAddedOrRemoved)
                        {
                            break;
                        }
                    }
                }
            }
            catch (System.Exception e) {
                Debug.LogException(e);
                return(null);
            }

            PropagateChanges(prefab);

            if (prefab != null && renameRoots && !string.IsNullOrEmpty(dataDirectory))
            {
                Debug.Log($"{prefab.name.Color( Color.green )}: Updating main assets and names in " +
                          dataDirectory.Color(Color.gray) + ".");
                var roots = Utils.FindAssetsOfType <AGXUnity.IO.RestoredAssetsRoot>(dataDirectory);
                foreach (var root in roots)
                {
                    var assets    = ObjectDb.GetAssets(dataDirectory, root.Type);
                    var mainAsset = System.Array.Find(assets,
                                                      asset => AssetDatabase.IsMainAsset(asset));
                    if (mainAsset == null)
                    {
                        continue;
                    }
                    else if (!(mainAsset is AGXUnity.IO.RestoredAssetsRoot))
                    {
                        AssetDatabase.SetMainObject(root, AssetDatabase.GetAssetPath(mainAsset));
                        mainAsset = root;
                    }

                    AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(root),
                                              AGXUnity.IO.RestoredAssetsRoot.FindName(prefab.name, root.Type));
                }

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            return(prefab);
        }