UserEditCtrl.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class UserEditCtrl : MonoBehaviour
  6. {
  7. public void Init(UserEditMgr userEditMgr, UserEditAPI userEditAPI, GameObject goUserEditPanel)
  8. {
  9. this.m_mgr = userEditMgr;
  10. this.m_goPanel = goUserEditPanel;
  11. this.m_userEditAPI = userEditAPI;
  12. }
  13. private void InitViewer()
  14. {
  15. this.m_name = UTY.GetChildObject(this.m_goPanel, "Name/InputField", false).GetComponent<UIInput>();
  16. this.m_nameBoxCollider = UTY.GetChildObject(this.m_goPanel, "Name/InputField", false).GetComponent<BoxCollider>();
  17. this.m_abdomenSlider = UTY.GetChildObject(this.m_goPanel, "Abdomen/Slider", false).GetComponent<UISlider>();
  18. this.m_abdomenValue = UTY.GetChildObject(this.m_goPanel, "Abdomen/Value/Number", false).GetComponent<UILabel>();
  19. this.m_colorRedSlider = UTY.GetChildObject(this.m_goPanel, "Color/R/Slider", false).GetComponent<UISlider>();
  20. this.m_colorGreenSlider = UTY.GetChildObject(this.m_goPanel, "Color/G/Slider", false).GetComponent<UISlider>();
  21. this.m_colorBlueSlider = UTY.GetChildObject(this.m_goPanel, "Color/B/Slider", false).GetComponent<UISlider>();
  22. this.m_goHeadPresetUnitParent = UTY.GetChildObject(this.m_goPanel, "Head/HeadPresetUnitParent", false);
  23. UIButton component = UTY.GetChildObject(this.m_goPanel, "Ok", false).GetComponent<UIButton>();
  24. EventDelegate.Add(component.onClick, new EventDelegate.Callback(this.m_mgr.CloseUserEditPanel));
  25. bool enabled = this.m_userEditAPI.FirstMan();
  26. this.m_nameBoxCollider.enabled = enabled;
  27. this.m_name.enabled = enabled;
  28. this.InitData();
  29. }
  30. private void InitData()
  31. {
  32. this.m_name.value = this.m_userEditAPI.GetName();
  33. this.m_abdomenSlider.value = this.m_userEditAPI.GetFat();
  34. this.m_abdomenValue.text = this.ToLabelValue(this.m_abdomenSlider.value, 100);
  35. this.m_colorRedSlider.value = this.m_userEditAPI.GetColor().r;
  36. this.m_colorGreenSlider.value = this.m_userEditAPI.GetColor().g;
  37. this.m_colorBlueSlider.value = this.m_userEditAPI.GetColor().b;
  38. }
  39. private string ToLabelValue(float input, int coefficient)
  40. {
  41. float num = (float)Math.Round((double)input, 2, MidpointRounding.AwayFromZero);
  42. float num2 = num * (float)coefficient;
  43. return ((int)num2).ToString();
  44. }
  45. public void CreateViewer(Dictionary<string, UserEditCtrl.HeadButton> dicHeadPreset)
  46. {
  47. if (!this.m_bInit)
  48. {
  49. this.InitViewer();
  50. this.m_bInit = true;
  51. }
  52. this.m_dicHeadPreset = dicHeadPreset;
  53. if (dicHeadPreset == null || dicHeadPreset.Count == 0)
  54. {
  55. return;
  56. }
  57. this.PrepareToCreateViewer(this.m_goHeadPresetUnitParent);
  58. this.ValidateHeadPreset(dicHeadPreset);
  59. this.m_goHeadPresetUnit = this.GetHeadPresetUnit();
  60. foreach (KeyValuePair<string, UserEditCtrl.HeadButton> keyValuePair in dicHeadPreset)
  61. {
  62. UserEditCtrl.HeadButton value = keyValuePair.Value;
  63. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_goHeadPresetUnit);
  64. this.SetTransformInfo(gameObject, this.m_goHeadPresetUnitParent);
  65. gameObject.name = value.name;
  66. UITexture component = gameObject.GetComponent<UITexture>();
  67. component.mainTexture = value.headIcon;
  68. UIButton component2 = gameObject.GetComponent<UIButton>();
  69. EventDelegate.Add(component2.onClick, new EventDelegate.Callback(this.m_mgr.ClickPreset));
  70. }
  71. BaseCreateViewerCtrl.Reposition(this.m_goHeadPresetUnitParent);
  72. }
  73. private void PrepareToCreateViewer(GameObject goParent)
  74. {
  75. IEnumerator enumerator = goParent.transform.GetEnumerator();
  76. try
  77. {
  78. while (enumerator.MoveNext())
  79. {
  80. object obj = enumerator.Current;
  81. Transform transform = (Transform)obj;
  82. UnityEngine.Object.Destroy(transform.gameObject);
  83. }
  84. }
  85. finally
  86. {
  87. IDisposable disposable;
  88. if ((disposable = (enumerator as IDisposable)) != null)
  89. {
  90. disposable.Dispose();
  91. }
  92. }
  93. goParent.transform.DetachChildren();
  94. }
  95. private void ValidateHeadPreset(Dictionary<string, UserEditCtrl.HeadButton> dicHeadPreset)
  96. {
  97. if (dicHeadPreset.Count > 10)
  98. {
  99. Debug.LogError(string.Format("頭のプリセット数が予定されている最大数を超えています。最大数={0}", 10));
  100. }
  101. }
  102. private GameObject GetHeadPresetUnit()
  103. {
  104. this.m_goHeadPresetUnit = (this.m_goHeadPresetUnit ?? (Resources.Load("SceneUserEdit/Prefab/HeadPresetUnit") as GameObject));
  105. return this.m_goHeadPresetUnit;
  106. }
  107. private void SetTransformInfo(GameObject copyPrefabs, GameObject goParent)
  108. {
  109. copyPrefabs.transform.parent = goParent.transform;
  110. copyPrefabs.transform.localScale = Vector3.one;
  111. copyPrefabs.transform.localPosition = Vector3.zero;
  112. copyPrefabs.transform.rotation = Quaternion.identity;
  113. }
  114. public void SetPreset(string btnName)
  115. {
  116. UserEditCtrl.HeadButton headButton = new UserEditCtrl.HeadButton();
  117. if (this.m_dicHeadPreset.TryGetValue(btnName, out headButton))
  118. {
  119. if (!this.m_userEditAPI.IsSelected(MPN.head, headButton.item))
  120. {
  121. this.m_userEditAPI.ProcMenu(headButton.item);
  122. }
  123. }
  124. else
  125. {
  126. Debug.LogError(string.Format("不適切なプリセット(頭)が選択されました。選択されたプリセット名={0}", btnName));
  127. }
  128. }
  129. public void UpdateAbdomenValue()
  130. {
  131. if (this.m_bInit)
  132. {
  133. float num = (float)Math.Round((double)this.m_abdomenSlider.value, 2, MidpointRounding.AwayFromZero);
  134. this.m_abdomenValue.text = this.ToLabelValue(num, 100);
  135. this.m_userEditAPI.SetFat(num);
  136. }
  137. }
  138. public void UpdateColorValue()
  139. {
  140. if (this.m_bInit)
  141. {
  142. this.m_userEditAPI.SetColor(new Color(this.m_colorRedSlider.value, this.m_colorGreenSlider.value, this.m_colorBlueSlider.value, 1f));
  143. }
  144. }
  145. public void SetName(string name)
  146. {
  147. this.m_userEditAPI.SetName(name);
  148. }
  149. public void SaveAndLoadScene()
  150. {
  151. this.m_userEditAPI.SetName(this.m_name.value);
  152. }
  153. private UserEditMgr m_mgr;
  154. private UserEditAPI m_userEditAPI;
  155. private Dictionary<string, UserEditCtrl.HeadButton> m_dicHeadPreset;
  156. private GameObject m_goPanel;
  157. private GameObject m_goHeadPresetUnit;
  158. private GameObject m_goHeadPresetUnitParent;
  159. private BoxCollider m_nameBoxCollider;
  160. private UIInput m_name;
  161. private UISlider m_abdomenSlider;
  162. private UISlider m_colorRedSlider;
  163. private UISlider m_colorGreenSlider;
  164. private UISlider m_colorBlueSlider;
  165. private UILabel m_abdomenValue;
  166. private bool m_bInit;
  167. private const string HEAD_PRESET_UNIT_PARENT_PATH = "Head/HeadPresetUnitParent";
  168. private const string HEAD_PRESET_UNIT_PATH = "SceneUserEdit/Prefab/HeadPresetUnit";
  169. private const string SCENE_NAME_FROM_USER_EDIT = "SceneEdit";
  170. private const int MAX_HEAD_PRESET_COUNT = 10;
  171. private const int ABDOMEN_COEFFICIENT = 100;
  172. private const int COLOR_COEFFICIENT = 255;
  173. private const int SLIDER_NUMBER_OF_DECIMAL_PLACES = 2;
  174. public class HeadButton
  175. {
  176. public string name;
  177. public Texture2D headIcon;
  178. public SceneEdit.SMenuItem item;
  179. }
  180. }