示例#1
0
        private void DrawSkeleton(Rect rect)
        {
            GUILayout.BeginArea(rect);
            {
                EditorGUILayout.BeginVertical();
                {
                    EGUILayout.DrawBoxHeader("Skeleton Data", EGUIStyles.BoxedHeaderCenterStyle, GUILayout.ExpandWidth(true));
                    if (currentCreatorData != null && skeletonCreatorDataDrawer != null)
                    {
                        skeletonCreatorDataDrawer.OnGUILayout();

                        AvatarSkeletonCreatorData skeletonCreatorData = currentCreatorData.skeletonData;

                        string     targetPrefabPath = skeletonCreatorData.GetSkeletonPrefabPath();
                        GameObject targetPrefab     = null;
                        if (!string.IsNullOrEmpty(targetPrefabPath))
                        {
                            targetPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(targetPrefabPath);
                        }
                        EditorGUILayout.Space();
                        EditorGUILayout.Space();

                        EditorGUILayout.ObjectField("Output", targetPrefab, typeof(GameObject), false);

                        EditorGUILayout.Space();

                        GUILayout.FlexibleSpace();

                        if (GUILayout.Button("Create Skeleton"))
                        {
                            GameObject skeletonPrefab = AvatarCreatorUtil.CreateSkeleton(skeletonCreatorData);
                            if (skeletonPrefab == null)
                            {
                                EditorUtility.DisplayDialog("Error", "Create Failed.\n Please view the details from the console!!!", "OK");
                            }
                            else
                            {
                                SelectionUtility.PingObject(skeletonPrefab);
                            }
                        }

                        if (GUILayout.Button("Preview Skeleton"))
                        {
                            PreviewSkeleton();
                        }
                    }
                }
                EditorGUILayout.EndVertical();
            }
            GUILayout.EndArea();
        }
示例#2
0
        private void PreviewSkeleton()
        {
            AvatarSkeletonCreatorData skeletonCreatorData = currentCreatorData.skeletonData;
            string skeletonAssetPath = skeletonCreatorData.GetSkeletonPrefabPath();

            if (AssetDatabaseUtility.IsAssetAtPath <GameObject>(skeletonAssetPath))
            {
                previewer.LoadSkeleton(skeletonAssetPath);
            }
            else
            {
                EditorUtility.DisplayDialog("Error", $"The prefab is not found in \"{skeletonAssetPath}\"", "OK");
            }
        }
示例#3
0
        public static GameObject CreateSkeleton(AvatarSkeletonCreatorData data)
        {
            if (data == null)
            {
                Debug.LogError("AvatarCreatorUtil::CreateSkeleton->the data is null");
                return(null);
            }

            if (data.fbx == null)
            {
                Debug.LogError("AvatarCreatorUtil::CreateSkeleton->The fbx is null");
                return(null);
            }

            PrefabAssetType assetType = UnityEditor.PrefabUtility.GetPrefabAssetType(data.fbx);

            if (assetType != PrefabAssetType.Model)
            {
                Debug.LogError($"AvatarCreatorUtil::CreateSkeleton->The fbx is not a model.type = {assetType}");
                return(null);
            }

            if (string.IsNullOrEmpty(data.outputFolder))
            {
                Debug.LogError("AvatarCreatorUtil::CreateSkeleton->The outputFolder is empty");
                return(null);
            }

            string outputDiskFolder = PathUtility.GetDiskPath(data.outputFolder);

            if (!Directory.Exists(outputDiskFolder))
            {
                Directory.CreateDirectory(outputDiskFolder);
            }

            string        skeletonPrefabAssetPath = data.GetSkeletonPrefabPath();
            GameObject    cachedPrefab            = AssetDatabase.LoadAssetAtPath <GameObject>(skeletonPrefabAssetPath);
            NodeBehaviour cachedNodeBehaviour     = cachedPrefab?.GetComponent <NodeBehaviour>();

            GameObject    instanceGameObject    = GameObject.Instantiate <GameObject>(data.fbx);
            NodeBehaviour instanceNodeBehaviour = instanceGameObject.AddComponent <NodeBehaviour>();

            instanceNodeBehaviour.FindBoneNodes();
            instanceNodeBehaviour.FindSMRendererNodes();
            if (cachedNodeBehaviour != null)
            {
                instanceNodeBehaviour.CopyFrom(cachedNodeBehaviour);
            }

            if (instanceNodeBehaviour.smRendererNodes != null && instanceNodeBehaviour.smRendererNodes.Length > 0)
            {
                foreach (var nodeData in instanceNodeBehaviour.smRendererNodes)
                {
                    SkinnedMeshRenderer smr = nodeData.renderer;
                    if (smr != null)
                    {
                        smr.sharedMaterials = new Material[0];
                        smr.rootBone        = null;
                        smr.sharedMesh      = null;
                        smr.bones           = new Transform[0];
                    }
                }
            }

            UnityEditor.PrefabUtility.SaveAsPrefabAsset(instanceGameObject, skeletonPrefabAssetPath);
            GameObject.DestroyImmediate(instanceGameObject);

            AssetDatabase.ImportAsset(skeletonPrefabAssetPath);

            return(AssetDatabase.LoadAssetAtPath <GameObject>(skeletonPrefabAssetPath));
        }