Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum MP_Anchor {TopLeft, TopRight, BottomLeft, BottomRight};
  2. enum MP_RT { Pixel, Percentage };
  3.  
  4. class MP_RectType {
  5.     var x : MP_RT = MP_RT.Pixel;
  6.     var y : MP_RT = MP_RT.Pixel;
  7.     var width : MP_RT = MP_RT.Pixel;
  8.     var height : MP_RT = MP_RT.Pixel;
  9. }
  10. class MP_Style {
  11.     var background : Texture2D;
  12.     var backgroundColor : Color = Color.clear;
  13.     var normalTexture : Texture2D;
  14.     var normalColor : Color = Color.clear;
  15.     var highlightTexture : Texture2D;
  16.     var highlightColor : Color = Color.white;
  17.     var textColor : Color = Color.black;
  18. }
  19.  
  20. class MP_Marg {
  21.     var top : float = 0;
  22.     var left : float = 0;
  23.     var right : float = 0;
  24.     var bottom : float = 0;
  25. }
  26.  
  27. class MP_PlayList {
  28.     var anchor : MP_Anchor = MP_Anchor.TopLeft;
  29.     var rect : Rect = Rect(0,0,300,300);
  30.     var rectType : MP_RectType;
  31.     var margin : MP_Marg;
  32.     var style : MP_Style;
  33. }
  34. var tracks : AudioClip[];
  35. var background : Texture2D;
  36. var playList : MP_PlayList;
  37.  
  38. //------------------------------------------------------------
  39. private var options = ['A','B','C','D','E','F'];
  40. private var selectedOption = 0;
  41. private var scrollPosition : Vector2 = new Vector2(0,0);
  42. private var playListStyle : GUIStyle = GUIStyle();
  43. private var bgStyle : GUIStyle = GUIStyle();
  44. private var playListRect : Rect;
  45. private var plTexture : Texture2D;
  46. private var isPl : boolean = false;
  47. private var playMode : int = 0;
  48. private var oldScreenSize : Vector2;
  49. //start--------------------------------------------------------------------------------
  50. function Start() {
  51.     OtherStart();
  52.     if (audio==null) gameObject.AddComponent(AudioSource);
  53.     audio.playOnAwake = false;
  54.     audio.clip = tracks[0];
  55.     options = new String[tracks.length];
  56.     for (i = 0;i<tracks.length;i++) {
  57.         options[i] = tracks[i].name;
  58.     }
  59.     PlayListStart();
  60. }
  61.  
  62. function OtherStart() {
  63.     bgStyle.normal.background = background;
  64. }
  65.  
  66. function PlayListStart() {
  67.     if (playList.style.normalTexture!=null) {
  68.         playListStyle.normal.background = playList.style.normalTexture;
  69.     } else {
  70.         playListStyle.normal.background = ColorToTexture2D(playList.style.normalColor);
  71.     }
  72.     if (playList.style.highlightTexture!=null) {
  73.         playListStyle.onNormal.background = playList.style.highlightTexture;
  74.     } else {
  75.         playListStyle.onNormal.background = ColorToTexture2D(playList.style.highlightColor);
  76.     }
  77.     playListStyle.normal.textColor = playList.style.textColor;
  78.     playListStyle.onNormal.textColor = playList.style.textColor;
  79.     if (playList.style.background!=null) {
  80.         plTexture = playList.style.background;
  81.     } else {
  82.         plTexture = ColorToTexture2D(playList.style.backgroundColor);
  83.     }
  84. }
  85. //main code-----------------------------------------------------------------------------
  86. function Update() {
  87.     if (playMode!=3 && !audio.isPlaying && isPl) {
  88.         selectedOption++;
  89.     }
  90.     if (selectedOption>=tracks.length) {
  91.         selectedOption = 0;
  92.         if (playMode==0) {
  93.             isPl = false;
  94.         }
  95.     }
  96.     if (isPl && !audio.isPlaying) {
  97.         audio.Play();
  98.     }
  99. }
  100.  
  101. function MouseClick(position:Vector2) {
  102.     Debug.Log("works");
  103.     position.x += Screen.width/2;
  104.     position.y += Screen.height/2;
  105.     var tempRect = playListRect;
  106.     tempRect.x -= playList.margin.left;
  107.     tempRect.y -= playList.margin.top;
  108.     tempRect.width -= (playList.margin.left+playList.margin.right);
  109.     tempRect.height -= (playList.margin.top+playList.margin.bottom);
  110.     if (tempRect.Contains(position)) {
  111.         isPl = true;
  112.         if (audio.clip!=tracks[selectedOption]) audio.clip = tracks[selectedOption];
  113.         audio.Play();
  114.     }
  115. }
  116. //gui------------------------------------------------------------------------------------
  117. function OnGUI() {
  118.     GUI.Label(Rect(0,0,Screen.width,Screen.height),"",bgStyle);
  119.     PlayList();
  120. }
  121.  
  122. function PlayList() {
  123.     var isClick = (Event.current.type == EventType.MouseUp && Event.current.clickCount == 1);
  124.     playListRect = RectGetter(playList.rect,playList.anchor,playList.rectType);
  125.     var marg = playList.margin;
  126.     var st = GUIStyle();
  127.     st.normal.background = plTexture;
  128.     GUILayout.BeginArea(playListRect,st);
  129.     GUILayout.BeginVertical("");
  130.     GUILayout.Space(marg.top);
  131.     GUILayout.BeginHorizontal("");
  132.     GUILayout.Space(marg.left);
  133.     scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);
  134.     selectedOption = GUILayout.SelectionGrid(selectedOption, options, 1, playListStyle);
  135.     if(isClick)
  136.         MouseClick(Event.current.mousePosition);
  137.     GUILayout.EndScrollView();
  138.     GUILayout.Space(marg.right);
  139.     GUILayout.EndHorizontal();
  140.     GUILayout.Space(marg.bottom);
  141.     GUILayout.EndVertical();
  142.     GUILayout.EndArea();
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. function RectGetter(rect:Rect,type:MP_Anchor,rt:MP_RectType) {
  150.     if (rt.x == MP_RT.Percentage) {
  151.         rect.x = Screen.width*rect.x;
  152.     }
  153.     if (rt.y == MP_RT.Percentage) {
  154.         rect.y = Screen.height*rect.y;
  155.     }
  156.     if (rt.width == MP_RT.Percentage) {
  157.         rect.width = Screen.width*rect.width;
  158.     }
  159.     if (rt.height == MP_RT.Percentage) {
  160.         rect.height = Screen.height*rect.height;
  161.     }
  162.     if (type == MP_Anchor.TopRight || type == MP_Anchor.BottomRight) {
  163.         rect.x = Screen.width-rect.x;
  164.     }
  165.     if (type == MP_Anchor.BottomLeft || type == MP_Anchor.BottomRight) {
  166.         rect.y = Screen.height-rect.y;
  167.     }
  168.     return rect;
  169. }
  170.  
  171.  
  172. private static var C2T_textures = new Hashtable();
  173. static function ColorToTexture2D(color : Color) : Texture2D
  174. {
  175.     var colorHash : int = color.GetHashCode();
  176.     //Debug.Log(colorHash);
  177.     var texture : Texture2D = C2T_textures[colorHash];
  178.     if(texture) return texture;
  179.     texture = new Texture2D(1,1);
  180.     texture.SetPixel(0,0,color);
  181.     texture.Apply();
  182.     C2T_textures[colorHash] = texture;
  183.     return texture;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement