Пример #1
0
        private static Part getMissingBodySkin(CharacterViewer character)
        {
            Dictionary <string, string> links = SetupData.partLinks[SlotCategory.BodySkin];
            List <Part> bodyskins             = PartList.Static.FindParts(SlotCategory.BodySkin);

            foreach (Part skin in bodyskins)
            {
                bool found = false;
                foreach (Sprite sprite in skin.sprites)
                {
                    try
                    {
                        found = character.transform.Find(links[sprite.name]).GetComponent <SpriteRenderer>().sprite == sprite;
                        if (!found)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        found = false;
                        break;
                    }
                }
                if (found)
                {
                    return(skin);
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// [EDITOR] Load CharacterViewer from a given path.
        /// </summary>
        /// <param name="path">The path must be in Assets directory.</param>
        /// <returns>Character object if it's existed, otherwise returns 'null'.</returns>
        public static CharacterViewer LoadCharacterFromPrefab(string path)
        {
            CharacterViewer val = null;

#if UNITY_EDITOR
            try
            {
                val = AssetDatabase.LoadAssetAtPath <CharacterViewer>(path);

                //version exception..
                if (val != null && val.GetAssignedPart(SlotCategory.BodySkin) == null)
                {
                    Part missingskin = getMissingBodySkin(val);
                    if (missingskin != null)
                    {
                        val.EquipPart(SlotCategory.BodySkin, missingskin);
                    }
                }
                //..version exception
            }
            catch (Exception e)
            {
                Debug.LogError("error on load from prefab:\n" + e.ToString());
            }
#endif
            return(val);
        }
Пример #3
0
 private List <SegmentData> getSegmentDatas(CharacterViewer character)
 {
     try
     {
         return(character.setupData.bodyTypeData.Find(d => d.bodyType == character.bodyType).segmentDatas);
     }
     catch
     {
         return(null);
     }
 }
Пример #4
0
        /// <summary>
        /// [Editor] Instantiate a CharacterViewer based on another CharacterViewer.
        /// </summary>
        /// <param name="baseCharacter">CharacterViewer to be copied from.</param>
        /// <param name="prefabPath">Path to the CharacterViewer prefab in Assets directory.</param>
        /// <returns>Instantiated CharacterViewer with the same settings as baseCharacter that linked to prefab in prefabPath. Returns an instantiated object of baseCharacter if there is no CharacterViewer found in prefabPath.</returns>
        public static CharacterViewer SpawnCharacter(CharacterViewer baseCharacter, string prefabPath)
        {
            CharacterViewer val = null;

            val = GameObject.Instantiate <CharacterViewer>(baseCharacter);
            if (val != null)
            {
                val.name = Path.GetFileNameWithoutExtension(prefabPath);
            }

            return(val);
        }
Пример #5
0
        private string getFullpath(CharacterViewer character)
        {
            Transform target = transform.parent;
            string    val    = target.name;

            while (target.parent != null && target.parent.gameObject != character.gameObject)
            {
                val    = string.Format("{0}/{1}", target.parent.name, val);
                target = target.parent;
            }
            return(val);
        }
Пример #6
0
        public void Detach(CharacterViewer character)
        {
            _fullpath = getFullpath(character);
            _locpos   = transform.localPosition;
            _locrot   = transform.localRotation;
            _locscale = transform.localScale;

            if (!hasParentEntity())
            {
                this.transform.SetParent(null);
            }
        }
Пример #7
0
        /// <summary>
        /// Initialize this BodySegmentScaler
        /// </summary>
        /// <param name="character">Character to be scaled.</param>
        public void Initialize(CharacterViewer character)
        {
            //..init _segmentdatas
            _segmentdatas = new Dictionary <SegmentType, SegmentData>();
            List <SegmentData> segmentdata = getSegmentDatas(character);

            if (segmentdata == null)
            {
                return;
            }
            foreach (SegmentData d in segmentdata)
            {
                _segmentdatas.Add(d.segmentType, d);
            }
            //init _segmentdatas..

            _scaledaffections = new Dictionary <SegmentType, ScaledAffection>();
            _posaffections    = new Dictionary <string, PositionedAffection>();
            foreach (SegmentType t in _segmentdatas.Keys)
            {
                //..init scaled affections
                _scaledaffections.Add(t, new ScaledAffection(character.transform, _segmentdatas[t].scaledObjects));
                //init scaled affections..

                //..init positioned affections
                foreach (PositionedObjectData d in _segmentdatas[t].positionedObjects)
                {
                    PositionedAffection taff = null;
                    if (_posaffections.ContainsKey(d.objectPath))
                    {
                        taff = _posaffections[d.objectPath];
                    }
                    else
                    {
                        Transform trans = character.transform.Find(d.objectPath);
                        taff = new PositionedAffection(trans);
                        _posaffections.Add(d.objectPath, taff);
                    }

                    taff.AddEntity(t, d);
                }
                //init positioned affections..
            }

            foreach (SegmentType t in _segmentdatas.Keys)
            {
                SetScale(t, GetScale(t));
            }
        }
Пример #8
0
        /// <summary>
        /// [EDITOR] Load CharacterViewer from a given path.
        /// </summary>
        /// <param name="path">The path must be in Assets directory.</param>
        /// <returns>Character object if it's existed, otherwise returns 'null'.</returns>
        public static CharacterViewer LoadCharacterFromPrefab(string path)
        {
            CharacterViewer val = null;

#if UNITY_EDITOR
            try
            {
                val = AssetDatabase.LoadAssetAtPath <CharacterViewer>(path);
            }
            catch (Exception e)
            {
                Debug.LogError("error on load from prefab:\n" + e.ToString());
            }
#endif
            return(val);
        }
Пример #9
0
        public void Attach(CharacterViewer character)
        {
            if (hasParentEntity())
            {
                return;
            }

            Transform targetparent = character.transform.Find(_fullpath);

            if (character.transform.Find(string.Format("{0}/{1}", _fullpath, this.name)))
            {
                Destroy(this.gameObject);
            }
            transform.SetParent(targetparent);
            transform.localPosition = _locpos;
            transform.localRotation = _locrot;
            transform.localScale    = _locscale;
        }
Пример #10
0
        /// <summary>
        /// [Editor] Instantiate a CharacterViewer based on another CharacterViewer.
        /// </summary>
        /// <param name="baseCharacter">CharacterViewer to be copied from.</param>
        /// <param name="prefabPath">Path to the CharacterViewer prefab in Assets directory.</param>
        /// <returns>Instantiated CharacterViewer with the same settings as baseCharacter that linked to prefab in prefabPath. Returns an instantiated object of baseCharacter if there is no CharacterViewer found in prefabPath.</returns>
        public static CharacterViewer SpawnCharacter(CharacterViewer baseCharacter, string prefabPath)
        {
            CharacterViewer val = null;

#if UNITY_EDITOR
            CharacterViewer sourcecharacter = LoadCharacterFromPrefab(prefabPath);
            if (sourcecharacter == null)
            {
                val      = GameObject.Instantiate <CharacterViewer>(baseCharacter);
                val.name = Path.GetFileNameWithoutExtension(prefabPath);
            }
            else
            {
                val      = GameObject.Instantiate <CharacterViewer>(sourcecharacter);
                val.name = sourcecharacter.name;
                val.AssignCharacterData(baseCharacter.GenerateCharacterData());
            }
#endif
            return(val);
        }
Пример #11
0
        /// <summary>
        /// [EDITOR] Save a CharacterViewer as a prefab in a selected path and automatically generate its materials.
        /// </summary>
        /// <param name="character">CharacterViewer to be saved as prefab.</param>
        /// <param name="path">The path must be in Assets directory.</param>
        /// <returns>'true' on succes, otherwise returns 'false'.</returns>
        public static bool SaveCharacterToPrefab(CharacterViewer sourceCharacter, string path)
        {
#if UNITY_EDITOR
            try
            {
                List <Material> materials = new List <Material>();

                //..get all character's materials
                foreach (SlotCategory c in Enum.GetValues(typeof(SlotCategory)))
                {
                    PartSlot slot = sourceCharacter.slots.GetSlot(c);
                    if (slot == null || slot.material == null || materials.Contains(slot.material))
                    {
                        continue;
                    }
                    slot.material.name = slot.material.name.Replace("CC2D", sourceCharacter.name);
                    materials.Add(slot.material);
                }
                //get all character's materials..

                string materialfolder = String.Format("{0}/{1}_materials", Path.GetDirectoryName(path), sourceCharacter.name);
                if (!Directory.Exists(materialfolder))
                {
                    Directory.CreateDirectory(materialfolder);
                    AssetDatabase.Refresh();
                }

                foreach (Material m in materials)
                {
                    string filename = String.Format("{0}/{1}.mat", materialfolder, m.name);
                    AssetDatabase.CreateAsset(m, filename);
                }
                AssetDatabase.Refresh();
                sourceCharacter.RelinkMaterials();

#if UNITY_2018_3_OR_NEWER
                if (AssetDatabase.LoadAssetAtPath <CharacterViewer>(path) != null)
                {
                    GameObject      targetgo    = PrefabUtility.LoadPrefabContents(path);
                    CharacterViewer targetchara = targetgo.GetComponent <CharacterViewer>();
                    Transform       sourceroot  = sourceCharacter.transform.Find("Root");
                    Transform       targetroot  = targetchara.transform.Find("Root");
                    if (targetroot != null)
                    {
                        GameObject.DestroyImmediate(targetroot.gameObject);
                    }
                    if (sourceroot != null)
                    {
                        targetroot      = GameObject.Instantiate <Transform>(sourceroot);
                        targetroot.name = "Root";
                        targetroot.SetParent(targetchara.transform);
                        targetroot.localPosition = Vector3.zero;
                        EditorUtility.CopySerialized(sourceCharacter, targetchara);
                        PrefabUtility.SaveAsPrefabAsset(targetgo, path);
                        PrefabUtility.UnloadPrefabContents(targetgo);
                    }
                }
                else
                {
                    PrefabUtility.SaveAsPrefabAsset(sourceCharacter.gameObject, path);
                }
#else
                CharacterViewer extcharacter = AssetDatabase.LoadAssetAtPath <CharacterViewer>(path);
                if (extcharacter != null)
                {
                    PrefabUtility.ReplacePrefab(sourceCharacter.gameObject, extcharacter);
                }
                else
                {
                    PrefabUtility.CreatePrefab(path, sourceCharacter.gameObject, ReplacePrefabOptions.ConnectToPrefab);
                }
#endif

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

                return(true);
            }
            catch (Exception e)
            {
                Debug.LogError("error on save character to prefab:\n" + e.ToString());
                return(false);
            }
#else
            return(false);
#endif
        }