GE_FantasySkyboxFREE_Demo.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #region Namespaces
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. #endregion // Namespaces
  6. public class GE_FantasySkyboxFREE_Demo : MonoBehaviour
  7. {
  8. #region Variables
  9. [System.Serializable]
  10. public class LightAndSky
  11. {
  12. // Name
  13. public string m_Name;
  14. // Light
  15. public Light m_Light;
  16. // Skybox
  17. public Material m_Skybox;
  18. // Fog
  19. public Color m_FogColor;
  20. // Ambient
  21. public Color m_AmbientLight;
  22. }
  23. // List of LightAndSky class
  24. public LightAndSky[] m_LightAndSkyList;
  25. // Index to current Skybox
  26. int m_CurrentSkyBox = 2;
  27. #endregion // Variables
  28. #region MonoBehaviour
  29. void Start ()
  30. {
  31. // Display first skybox in m_LightAndSkyList
  32. SetSkyBox(m_CurrentSkyBox);
  33. // Update UI Text elements
  34. //UpdateDetailsText();
  35. //UpdateHowToText();
  36. }
  37. void Update ()
  38. {
  39. // User press Left key
  40. //if(Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.A))
  41. //{
  42. // // Show previous skybox
  43. // OnPreviousSkybox();
  44. //}
  45. //// User press Right key
  46. //if(Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.D))
  47. //{
  48. // // Show next skybox
  49. // OnNextSkybox();
  50. //}
  51. }
  52. void OnTriggerExit(Collider other)
  53. {
  54. Debug.Log("OnTriggerExit="+other.name);
  55. // Reset player position when user move it away from terrain
  56. this.transform.localPosition = new Vector3(0,1,0);
  57. }
  58. #endregion // MonoBehaviour
  59. // ########################################
  60. // Switch skybox functions
  61. // ########################################
  62. #region Switch skybox functions
  63. // Switch to previous skybox
  64. public void OnPreviousSkybox()
  65. {
  66. SwitchSkyBox(-1);
  67. //UpdateDetailsText();
  68. }
  69. // Switch to next skybox
  70. public void OnNextSkybox()
  71. {
  72. SwitchSkyBox(+1);
  73. //UpdateDetailsText();
  74. }
  75. #endregion // MonoBehaviour
  76. // ########################################
  77. // Show skybox functions
  78. // ########################################
  79. #region Show skybox functions
  80. // Switch to a skybox by direction
  81. // DiffNum less than 0 means previous skybox
  82. // DiffNum larger than 0 means next skybox
  83. void SwitchSkyBox(int DiffNum)
  84. {
  85. // Update add m_CurrentSkyBox with DiffNum
  86. m_CurrentSkyBox += DiffNum;
  87. // Clamp m_CurrentSkyBox between 0 and m_LightAndSkyList.Length
  88. if(m_CurrentSkyBox<0)
  89. {
  90. m_CurrentSkyBox = m_LightAndSkyList.Length-1;
  91. }
  92. if(m_CurrentSkyBox>=m_LightAndSkyList.Length)
  93. {
  94. m_CurrentSkyBox = 0;
  95. }
  96. // Switch skybox in RenderSettings
  97. RenderSettings.skybox = m_LightAndSkyList[m_CurrentSkyBox].m_Skybox;
  98. // Switch light
  99. for(int i=0;i<m_LightAndSkyList.Length;i++)
  100. {
  101. m_LightAndSkyList[i].m_Light.gameObject.SetActive(false);
  102. }
  103. m_LightAndSkyList[m_CurrentSkyBox].m_Light.gameObject.SetActive(true);
  104. // Enable fog
  105. RenderSettings.fog = true;
  106. // Set the fog color
  107. if(m_CurrentSkyBox>=0 && m_CurrentSkyBox<m_LightAndSkyList.Length)
  108. {
  109. RenderSettings.fogColor = m_LightAndSkyList[m_CurrentSkyBox].m_FogColor;
  110. }
  111. else
  112. {
  113. RenderSettings.fogColor = Color.white;
  114. }
  115. // Set the ambient lighting
  116. if(m_CurrentSkyBox>=0 && m_CurrentSkyBox<m_LightAndSkyList.Length)
  117. {
  118. RenderSettings.ambientLight = m_LightAndSkyList[m_CurrentSkyBox].m_AmbientLight;
  119. }
  120. else
  121. {
  122. RenderSettings.ambientLight = Color.white;
  123. }
  124. }
  125. void SetSkyBox(int DiffNum)
  126. {
  127. // Update add m_CurrentSkyBox with DiffNum
  128. m_CurrentSkyBox = DiffNum;
  129. // Clamp m_CurrentSkyBox between 0 and m_LightAndSkyList.Length
  130. if (m_CurrentSkyBox < 0)
  131. {
  132. m_CurrentSkyBox = m_LightAndSkyList.Length - 1;
  133. }
  134. if (m_CurrentSkyBox >= m_LightAndSkyList.Length)
  135. {
  136. m_CurrentSkyBox = 0;
  137. }
  138. // Switch skybox in RenderSettings
  139. RenderSettings.skybox = m_LightAndSkyList[m_CurrentSkyBox].m_Skybox;
  140. // Switch light
  141. for (int i = 0; i < m_LightAndSkyList.Length; i++)
  142. {
  143. m_LightAndSkyList[i].m_Light.gameObject.SetActive(false);
  144. }
  145. m_LightAndSkyList[m_CurrentSkyBox].m_Light.gameObject.SetActive(true);
  146. // Enable fog
  147. RenderSettings.fog = true;
  148. // Set the fog color
  149. if (m_CurrentSkyBox >= 0 && m_CurrentSkyBox < m_LightAndSkyList.Length)
  150. {
  151. RenderSettings.fogColor = m_LightAndSkyList[m_CurrentSkyBox].m_FogColor;
  152. }
  153. else
  154. {
  155. RenderSettings.fogColor = Color.white;
  156. }
  157. // Set the ambient lighting
  158. if (m_CurrentSkyBox >= 0 && m_CurrentSkyBox < m_LightAndSkyList.Length)
  159. {
  160. RenderSettings.ambientLight = m_LightAndSkyList[m_CurrentSkyBox].m_AmbientLight;
  161. }
  162. else
  163. {
  164. RenderSettings.ambientLight = Color.white;
  165. }
  166. }
  167. #endregion // Show skybox functions
  168. // ########################################
  169. // Update UI text functions
  170. // ########################################
  171. //#region Update UI text functions
  172. //// Update details UI Text
  173. //void UpdateDetailsText()
  174. //{
  175. // // Update ItemNum text
  176. // GameObject Text_ItemNum = GameObject.Find("Text_ItemNum");
  177. // if(Text_ItemNum!=null)
  178. // {
  179. // Text pText = Text_ItemNum.GetComponent<Text>();
  180. // pText.text = string.Format("{0:00} of {1:00}",m_CurrentSkyBox+1, m_LightAndSkyList.Length);
  181. // }
  182. // // Update Details text
  183. // GameObject Text_Details = GameObject.Find("Text_Details");
  184. // if(Text_Details!=null)
  185. // {
  186. // Text pText = Text_Details.GetComponent<Text>();
  187. // pText.text = string.Format(m_LightAndSkyList[m_CurrentSkyBox].m_Name);
  188. // }
  189. //}
  190. //// Update how to UI Text
  191. //void UpdateHowToText()
  192. //{
  193. // // Find Text_HowTo in the scene
  194. // GameObject Text_HowTo = GameObject.Find("Text_HowTo");
  195. // if(Text_HowTo!=null)
  196. // {
  197. // // Update text according to target platform
  198. // if(Application.platform == RuntimePlatform.IPhonePlayer ||
  199. // Application.platform == RuntimePlatform.Android)// ||
  200. // //Application.platform == RuntimePlatform.BlackBerryPlayer ||
  201. // //Application.platform == RuntimePlatform.WSAPlayer)
  202. // {
  203. // Text pText = Text_HowTo.GetComponent<Text>();
  204. // pText.text = "Move: Joystick on left | Look: Joystick on right | Change Skybox: Tap";
  205. // }
  206. // else
  207. // {
  208. // Text pText = Text_HowTo.GetComponent<Text>();
  209. // pText.text = "Switch Skybox: Left/Right Arrow | Turn: Mouse Drag | Release Mouse: ESC Button";
  210. // }
  211. // }
  212. //}
  213. //#endregion // Update UI text functions
  214. }