private SlotCategory getMaterialsCategory(Material material) { foreach (SlotCategory s in Enum.GetValues(typeof(SlotCategory))) { PartSlot slot = slots.GetSlot(s); if (slot == null || slot.material == null) { continue; } if (material.name.Contains(slot.material.name)) { return(s); } } foreach (MaterialData md in setupData.defaultMaterials) { if (material.name.Contains(md.material.name)) { return(md.slotCategory); } } return(SlotCategory.Nose); }
private void instantiateSlotsMaterial() { Dictionary <string, Material> materials = new Dictionary <string, Material>(); foreach (SlotCategory cat in Enum.GetValues(typeof(SlotCategory))) { PartSlot slot = this.slots.GetSlot(cat); if (slot == null || slot.material == null) { continue; } if (!materials.ContainsKey(slot.material.name)) { Material tmat = Instantiate <Material>(slot.material); tmat.name = slot.material.name; materials.Add(tmat.name, tmat); slot.material = tmat; } else { slot.material = materials[slot.material.name]; } } RelinkMaterials(); }
private void relinkMaterials <T>() where T : Renderer { T[] renderers = this.transform.GetComponentsInChildren <T>(true); foreach (T r in renderers) { if (r.sharedMaterial == null) { continue; } PartSlot slot = slots.GetSlot(getMaterialsCategory(r.sharedMaterial)); if (slot == null || slot.material == null) { continue; } if (Application.isPlaying) { r.material = slot.material; } else { r.sharedMaterial = slot.material; } } }
private void equipSkinDetails(Part part) { if (part != null && part.category != PartCategory.SkinDetails) { return; } PartSlot slot = this.slots.GetSlot(SlotCategory.SkinDetails); if (slot == null) { return; } slot.assignedPart = part; if (slot.material != null) { if (part == null) { slot.material.SetTexture("_Details", null); } else { slot.material.SetTexture("_Details", part.texture); ResourcesManager.ReleaseZeroReference(); } } }
private void resetWeaponRenderer(PartSlot slot) { Weapon weapon = (Weapon)slot.assignedPart; if (weapon == null) { return; } if (slot == this.slots.GetSlot(SlotCategory.MainHand)) { foreach (string k in SetupData.rWeaponLink.Keys) { this.transform.Find(SetupData.rWeaponLink[k]).GetComponent <SpriteRenderer>().sprite = null; } } else if (slot == this.slots.GetSlot(SlotCategory.OffHand)) { foreach (string k in SetupData.bowLink.Keys) { this.transform.Find(SetupData.bowLink[k]).GetComponent <SpriteRenderer>().sprite = null; } foreach (string k in SetupData.shieldLink.Keys) { this.transform.Find(SetupData.shieldLink[k]).GetComponent <SpriteRenderer>().sprite = null; } foreach (string k in SetupData.lWeaponLink.Keys) { this.transform.Find(SetupData.lWeaponLink[k]).GetComponent <SpriteRenderer>().sprite = null; } } }
private void setSkinDetailsColor(Color color) { PartSlot slot = this.slots.GetSlot(SlotCategory.SkinDetails); slot.material.SetColor("_DetailsColor", color); slot.color1 = slot.color2 = slot.color3 = color; }
private void equipSkinDetails(Part part) { if (part != null && part.category != PartCategory.SkinDetails) { return; } PartSlot slot = this.slots.GetSlot(SlotCategory.SkinDetails); if (slot == null) { return; } slot.assignedPart = part; if (slot.material != null) { if (part == null) { slot.material.SetTexture("_Details", null); } else { #if CC2D_DEMO PartAssetManager.SetTexture(slot.assignedPart, slot.material, PartAssetManager.AssignAssetType.Details); #else slot.material.SetTexture("_Details", part.texture); #endif } } }
/// <summary> /// Returns the assigned part of a given SlotCategory. Returns 'null' if there is no part assigned. /// </summary> /// <param name="category">Given SlotCategory.</param> /// <returns>Assigned part of the given SlotCategory if there is any, otherwise returns 'null'.</returns> public Part GetAssignedPart(SlotCategory category) { PartSlot slot = this.slots.GetSlot(category); if (slot == null) { return(null); } return(slot.assignedPart); }
private void equipPart(SlotCategory slotCategory, Part part) { if (!SetupData.partLinks.ContainsKey(slotCategory)) { return; } Dictionary <string, string> links = SetupData.partLinks[slotCategory]; PartSlot slot = this.slots.GetSlot(slotCategory); if (links == null || slot == null) { return; } if (part != null && (int)slotCategory != (int)part.category) { Debug.Log("can't equip " + part.name + ". part doesn't match with slot category"); return; } //..reset part foreach (string k in links.Keys) { this.transform.Find(links[k]).GetComponent <SpriteRenderer>().sprite = null; } if (part == null) { slot.assignedPart = null; if (SetupData.colorableSpriteLinks.ContainsKey(slotCategory)) { SetPartColor(slotCategory, ColorCode.Color1, slot.color1); } return; } slot.assignedPart = part; if (slot.material != null) { slot.material.SetTexture("_ColorMask", part.colorMask); } { foreach (Sprite s in part.sprites) { this.transform.Find(links[s.name]).GetComponent <SpriteRenderer>().sprite = s; } ResourcesManager.ReleaseZeroReference(); } if (SetupData.colorableSpriteLinks.ContainsKey(slotCategory)) { SetPartColor(slotCategory, ColorCode.Color1, slot.color1); } }
private void refreshPartSlots() { foreach (SlotCategory cat in Enum.GetValues(typeof(SlotCategory))) { PartSlot s = this.slots.GetSlot(cat); EquipPart(cat, s.assignedPart); if (!SetupData.colorableSpriteLinks.ContainsKey(cat)) { SetPartColor(cat, ColorCode.Color1, s.color1); SetPartColor(cat, ColorCode.Color2, s.color2); SetPartColor(cat, ColorCode.Color3, s.color3); } } }
/// <summary> /// Repaint tint color according to the current value. /// </summary> public void RepaintTintColor() { foreach (SlotCategory c in Enum.GetValues(typeof(SlotCategory))) { PartSlot slot = slots.GetSlot(c); if (slot == null || slot.material == null) { continue; } if (slot.material.HasProperty("_Color")) { slot.material.SetColor("_Color", _tintcolor); } } }
private void equipSkirt(Part part) { if (part != null && part.category != PartCategory.Skirt) { return; } PartSlot slot = this.slots.GetSlot(SlotCategory.Skirt); if (slot == null) { return; } Transform capetransform = this.transform.Find("Root/Pos_Hip/Bone_Hip/Skirt"); slot.assignedPart = part; if (part == null) { if (slot.material != null) { slot.material.SetTexture("_MainTex", null); slot.material.SetTexture("_ColorMask", null); } if (capetransform != null) { capetransform.gameObject.SetActive(false); } } else { if (slot.material != null) { #if CC2D_DEMO PartAssetManager.SetTexture(slot.assignedPart, slot.material, PartAssetManager.AssignAssetType.MainTex); PartAssetManager.SetTexture(slot.assignedPart, slot.material, PartAssetManager.AssignAssetType.ColorMask); #else slot.material.SetTexture("_MainTex", part.texture); slot.material.SetTexture("_ColorMask", part.colorMask); #endif } if (capetransform != null) { capetransform.gameObject.SetActive(true); } } }
private void equipSkirt(Part part) { if (part != null && part.category != PartCategory.Skirt) { return; } PartSlot slot = this.slots.GetSlot(SlotCategory.Skirt); if (slot == null) { return; } Transform capetransform = this.transform.Find("Root/Pos_Hip/Bone_Hip/Skirt"); slot.assignedPart = part; if (part == null) { if (slot.material != null) { slot.material.SetTexture("_MainTex", null); slot.material.SetTexture("_ColorMask", null); } if (capetransform != null) { capetransform.gameObject.SetActive(false); } } else { if (slot.material != null) { slot.material.SetTexture("_MainTex", part.texture); slot.material.SetTexture("_ColorMask", part.colorMask); ResourcesManager.ReleaseZeroReference(); } if (capetransform != null) { capetransform.gameObject.SetActive(true); } } }
private void equipCape(Part part) { if (part != null && part.category != PartCategory.Cape) { return; } PartSlot slot = this.slots.GetSlot(SlotCategory.Cape); if (slot == null) { return; } Transform capetransform = this.transform.Find("Root/Pos_Hip/Bone_Hip/Pos_Body/Bone_Body/Cape"); slot.assignedPart = part; if (part == null) { if (slot.material != null) { slot.material.SetTexture("_MainTex", null); slot.material.SetTexture("_ColorMask", null); } if (capetransform != null) { capetransform.gameObject.SetActive(false); } } else { if (slot.material != null) { slot.material.SetTexture("_MainTex", part.texture); slot.material.SetTexture("_ColorMask", part.colorMask); } if (capetransform != null) { capetransform.gameObject.SetActive(true); } } }
private void setPartColor(SlotCategory slotCategory, string colorCode, Color color) { PartSlot slot = this.slots.GetSlot(slotCategory); if (SetupData.colorableSpriteLinks.ContainsKey(slotCategory)) { List <string> links = SetupData.colorableSpriteLinks[slotCategory]; foreach (string l in links) { this.transform.Find(l).GetComponent <SpriteRenderer>().color = color; } slot.color1 = slot.color2 = slot.color3 = color; return; } if (slot == null || slot.material == null) { return; } slot.material.SetColor(colorCode, color); switch (colorCode) { case ColorCode.Color1: slot.color1 = color; break; case ColorCode.Color2: slot.color2 = color; break; case ColorCode.Color3: slot.color3 = color; break; default: break; } }
/// <summary> /// Returns Part's Color from desired PartSlot. /// </summary> /// <param name="slotCategory">Desired SlotCategory</param> /// <param name="colorCode">Represent the desired color order of the part. Use 'CharacterCreator2D.ColorCode'.</param> /// <returns>Part's Color. Returns 'Color.clear' if colorCode doesn't match with any value in 'CharacterCreator2D.ColorCode'.</returns> public Color GetPartColor(SlotCategory slotCategory, string colorCode) { PartSlot slot = slots.GetSlot(slotCategory); if (slot != null) { switch (colorCode) { case ColorCode.Color1: return(slot.color1); case ColorCode.Color2: return(slot.color2); case ColorCode.Color3: return(slot.color3); default: return(Color.clear); } } return(Color.clear); }
/// <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 }
/// <summary> /// Generate CharacterData of CharacterViewer. /// </summary> /// <returns>Generated CharacterData of CharacterViewer.</returns> public CharacterData GenerateCharacterData() { CharacterData val = new CharacterData(); val.dataVersion = setupData.dataVersion; val.bodyType = this.bodyType; val.skinColor = _skincolor; val.tintColor = _tintcolor; val.slotData = new List <PartSlotData>(); foreach (SlotCategory cat in Enum.GetValues(typeof(SlotCategory))) { PartSlot slot = this.slots.GetSlot(cat); val.slotData.Add(new PartSlotData() { category = cat, partName = slot.assignedPart == null ? "" : slot.assignedPart.name, partPackage = slot.assignedPart == null ? "" : slot.assignedPart.packageName, color1 = slot.color1, color2 = slot.color2, color3 = slot.color3 }); } val.emoteData = new List <EmoteIndexData>(); foreach (EmotionType emotionType in Enum.GetValues(typeof(EmotionType))) { EmoteIndex e = emotes.getIndex(emotionType); if (e == null) { continue; } if (emotionType >= EmotionType.Blink && String.IsNullOrEmpty(e.name)) { continue; } else { val.emoteData.Add(new EmoteIndexData() { emotionType = emotionType, emotionName = String.IsNullOrEmpty(e.name) ? "" : e.name, eyebrowPartName = e.eyebrowPart == null ? "" : e.eyebrowPart.name, eyebrowPackage = e.eyebrowPart == null ? "" : e.eyebrowPart.packageName, eyesPartName = e.eyesPart == null ? "" : e.eyesPart.name, eyesPackage = e.eyesPart == null ? "" : e.eyesPart.packageName, nosePartName = e.nosePart == null ? "" : e.nosePart.name, nosePackage = e.nosePart == null ? "" : e.nosePart.packageName, mouthPartName = e.mouthPart == null ? "" : e.mouthPart.name, mouthPackage = e.mouthPart == null ? "" : e.mouthPart.packageName, earPartName = e.earPart == null ? "" : e.earPart.name, earPackage = e.earPart == null ? "" : e.earPart.packageName }); } } //..update val.bodySegmentData = new List <SegmentScaleData>(); foreach (SegmentType stype in Enum.GetValues(typeof(SegmentType))) { Vector2 scale = GetBodySlider(stype); val.bodySegmentData.Add(new SegmentScaleData() { segmentType = stype, scale = new Vector2(scale.x, scale.y) }); } //update.. return(val); }
/// <summary> /// Assign and initialize this CharacterViewer according to a given CharacterData. /// </summary> /// <param name="data">CharacterData to be assigned from.</param> public void AssignCharacterData(CharacterData data) { //data version exception.. if (data.dataVersion < 1) { data.slotData.Add(new PartSlotData() { category = SlotCategory.BodySkin, partName = data.bodyType == BodyType.Male ? "Base 00 Male" : "Base 00 Female", partPackage = "Base" }); } //..data version exception this.bodyType = data.bodyType; _skincolor = data.skinColor; _tintcolor = data.tintColor; Initialize(); foreach (SlotCategory cat in Enum.GetValues(typeof(SlotCategory))) { PartSlot slot = this.slots.GetSlot(cat); PartSlotData slotdata = data.slotData.Find(x => x.category == cat); if (slot == null) { continue; } if (string.IsNullOrEmpty(slotdata.partName)) { EquipPart(cat, (Part)null); continue; } Part part = PartList.Static.FindPart(slotdata.partName, slotdata.partPackage, cat); if (part == null) { continue; } EquipPart(cat, part); SetPartColor(cat, ColorCode.Color1, slotdata.color1); SetPartColor(cat, ColorCode.Color2, slotdata.color2); SetPartColor(cat, ColorCode.Color3, slotdata.color3); } if (data.emoteData != null || data.emoteData.Count > 0) { foreach (EmotionType emotionType in Enum.GetValues(typeof(EmotionType))) { EmoteIndex e = emotes.getIndex(emotionType); EmoteIndexData edata = data.emoteData.Find(x => x.emotionType == emotionType); if (e == null) { continue; } if (emotionType >= EmotionType.Blink && String.IsNullOrEmpty(edata.emotionName)) { continue; } e.name = edata.emotionName; if (string.IsNullOrEmpty(edata.eyebrowPartName)) { e.eyebrowPart = null; } else { e.eyebrowPart = PartList.Static.FindPart(edata.eyebrowPartName, edata.eyebrowPackage, SlotCategory.Eyebrow); } if (string.IsNullOrEmpty(edata.eyesPartName)) { e.eyesPart = null; } else { e.eyesPart = PartList.Static.FindPart(edata.eyesPartName, edata.eyesPackage, SlotCategory.Eyes); } if (string.IsNullOrEmpty(edata.nosePartName)) { e.nosePart = null; } else { e.nosePart = PartList.Static.FindPart(edata.nosePartName, edata.nosePackage, SlotCategory.Nose); } if (string.IsNullOrEmpty(edata.mouthPartName)) { e.mouthPart = null; } else { e.mouthPart = PartList.Static.FindPart(edata.mouthPartName, edata.mouthPackage, SlotCategory.Mouth); } if (string.IsNullOrEmpty(edata.earPartName)) { e.earPart = null; } else { e.earPart = PartList.Static.FindPart(edata.earPartName, edata.earPackage, SlotCategory.Ear); } } } //..update if (data.bodySegmentData == null || data.bodySegmentData.Count <= 0) { foreach (SegmentType st in Enum.GetValues(typeof(SegmentType))) { SetBodySlider(st, new Vector2(0.5f, 0.5f)); } } else { foreach (SegmentScaleData sd in data.bodySegmentData) { SetBodySlider(sd.segmentType, sd.scale); } } //update.. }
private void equipWeapon(SlotCategory slotCategory, Weapon weapon) { PartSlot slot = this.slots.GetSlot(slotCategory); if (slot == null) { return; } resetWeaponRenderer(slot); if (weapon == null) { slot.assignedPart = null; return; } Dictionary <string, string> links = new Dictionary <string, string>(); switch (slotCategory) { case SlotCategory.MainHand: if (weapon.weaponCategory == WeaponCategory.OneHanded || weapon.weaponCategory == WeaponCategory.TwoHanded || weapon.weaponCategory == WeaponCategory.Gun || weapon.weaponCategory == WeaponCategory.Rifle) { links = SetupData.rWeaponLink; } break; case SlotCategory.OffHand: if (weapon.weaponCategory == WeaponCategory.Bow) { links = SetupData.bowLink; } else if (weapon.weaponCategory == WeaponCategory.Shield) { links = SetupData.shieldLink; } else if (weapon.weaponCategory == WeaponCategory.OneHanded || weapon.weaponCategory == WeaponCategory.Gun) { links = SetupData.lWeaponLink; } break; default: break; } slot.assignedPart = weapon; if (slot.material != null) { slot.material.SetTexture("_ColorMask", weapon.colorMask); } { foreach (Sprite s in weapon.sprites) { this.transform.Find(links[s.name]).GetComponent <SpriteRenderer>().sprite = s; } ResourcesManager.ReleaseZeroReference(); } if (SetupData.colorableSpriteLinks.ContainsKey(slotCategory)) { SetPartColor(slotCategory, ColorCode.Color1, slot.color1); } Transform muzzlefx = null; if (slotCategory == SlotCategory.MainHand) { muzzlefx = this.transform.Find(SetupData.muzzleFXLinks[SlotCategory.MainHand]); } if (slotCategory == SlotCategory.OffHand) { muzzlefx = this.transform.Find(SetupData.muzzleFXLinks[SlotCategory.OffHand]); } muzzlefx.localPosition = weapon.muzzlePosition; }