示例#1
0
        private CreateObjectModel.AssetExtras ParseGLTF(byte[] gltfBinary)
        {
            var    json    = Encoding.UTF8.GetString(gltfBinary);
            var    jObject = JObject.Parse(json);
            string extras  = "";

            try
            {
                extras = jObject["asset"]["extras"].ToString();
            }
            catch
            {
                Debug.LogError("cannot import; no authoring data can be found in the .gltf");
            }

            CreateObjectModel.AssetExtras licenseInfo =
                JsonConvert.DeserializeObject <CreateObjectModel.AssetExtras>(extras);

            return(licenseInfo);
        }
示例#2
0
        private static GameObject CreateNew(GameObject obj, string localPath, string guid,
                                            string objectName, string classFullName, CreateObjectModel.AssetExtras licenseData,
                                            bool withMainClass = true,
                                            bool overwrite     = false)
        {
            if (withMainClass)
            {
                CreateObjectUtils.AddComponent(obj, localPath, classFullName,
                                               $"{objectName}");
            }

            var config = obj.GetComponent <VarwinObjectDescriptor>();

            if (config == null)
            {
                config = obj.AddComponent <VarwinObjectDescriptor>();

                if (config == null)
                {
                    obj    = Instantiate(obj);
                    config = obj.AddComponent <VarwinObjectDescriptor>();
                }
            }

            if (string.IsNullOrWhiteSpace(config.Guid))
            {
                config.Guid     = guid;
                config.RootGuid = guid;

                config.Name   = objectName;
                config.Prefab = localPath;

                config.AuthorName  = _authorName;
                config.AuthorEmail = _authorEmail;
                config.AuthorUrl   = _authorUrl;

                config.LicenseCode = _licenseCode;

                config.BuiltAt = DateTimeOffset.Now.ToString();

                config.MobileReady = SdkSettings.MobileFeature.Enabled && _mobileReady;
            }

            if (licenseData != null)
            {
                Regex reg        = new Regex(@"(.+)\s+\((.+)\)");
                var   authorData = reg.Match(licenseData.Author);

                try
                {
                    config.AuthorName = authorData.Groups[1].Value;
                    config.AuthorUrl  = authorData.Groups[2].Value;
                }
                catch
                {
                    Debug.LogWarning("cannot read author name and author url properties");
                }

                reg = new Regex(@"([a-zA-Z-]+)-([0-9\.]+)\s+\((.+)\)");
                var license = reg.Match(licenseData.License);

                try
                {
                    config.LicenseCode = license.Groups[1].Value.ToLower();
                }
                catch
                {
                    Debug.LogWarning("cannot read license code property");
                }
            }

            GameObject prefab;

            if (overwrite)
            {
                prefab = obj;
            }
            else
            {
                Object instanceRoot   = PrefabUtility.InstantiatePrefab(obj);
                Object prefabInstance = PrefabUtility.GetCorrespondingObjectFromSource(obj);

                bool isPrefabInstance = (instanceRoot == null);

                if (isPrefabInstance)
                {
                    instanceRoot = PrefabUtility.InstantiatePrefab(prefabInstance);
                }

                bool success = false;

                if (instanceRoot != null)
                {
                    GameObject fixObject = null;

                    if (!isPrefabInstance)
                    {
                        fixObject = (GameObject)PrefabUtility.InstantiatePrefab(obj);;
                    }

                    PrefabUtility.UnpackPrefabInstance((GameObject)instanceRoot,
                                                       PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
                    prefab = PrefabUtility.SaveAsPrefabAsset((GameObject)instanceRoot, localPath, out success);

                    if (!isPrefabInstance)
                    {
                        var objectDescriptor = fixObject.GetComponent <VarwinObjectDescriptor>();

                        if (objectDescriptor != null)
                        {
                            DestroyImmediate(objectDescriptor);
                            PrefabUtility.ApplyPrefabInstance(fixObject, InteractionMode.AutomatedAction);
                        }

                        DestroyImmediate(fixObject);
                    }

                    DestroyImmediate(instanceRoot);
                }
                else
                {
                    prefab = PrefabUtility.SaveAsPrefabAsset(obj, localPath, out success);
                    DestroyImmediate(obj);
                }

                EditorGUIUtility.PingObject(prefab);

                if (success)
                {
                    if (isPrefabInstance)
                    {
                        DestroyImmediate(obj);
                    }

                    return(prefab);
                }

                PrefabUtility.RevertPrefabInstance(obj, InteractionMode.AutomatedAction);

                Debug.LogError("Can not create prefab!");

                return(null);
            }

            return(prefab);
        }
示例#3
0
        public static GameObject CreatePrefab(GameObject gameObject, string localPath, string guid,
                                              string objectName, string classFullName, CreateObjectModel.AssetExtras licenseData,
                                              bool withMainClass = true)
        {
            if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))
            {
                if (EditorUtility.DisplayDialog("Are you sure?",
                                                $"{objectName} prefab already exists. Do you want to overwrite it?", "Yes", "No"))
                {
                    return(CreateNew(gameObject, localPath, guid,
                                     objectName, classFullName, licenseData,
                                     withMainClass,
                                     true));
                }
            }

            else
            {
                Debug.Log("Converting " + gameObject.name + " to prefab.");

                return(CreateNew(gameObject, localPath, guid,
                                 objectName, classFullName, licenseData,
                                 withMainClass));
            }

            return(null);
        }