Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
59
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. //start--------------------------------------------------------------------------------
  49. function Start() {
  50.     OtherStart();
  51.     if (audio==null) gameObject.AddComponent(AudioSource);
  52.     audio.playOnAwake = false;
  53.     audio.clip = tracks[0];
  54.     options = new String[tracks.length];
  55.     for (i = 0;i<tracks.length;i++) {
  56.         options[i] = tracks[i].name;
  57.     }
  58.     PlayListStart();
  59. }
  60.  
  61. function OtherStart() {
  62.     bgStyle.normal.background = background;
  63. }
  64.  
  65. function PlayListStart() {
  66.     if (playList.style.normalTexture!=null) {
  67.         playListStyle.normal.background = playList.style.normalTexture;
  68.     } else {
  69.         playListStyle.normal.background = ColorToTexture2D(playList.style.normalColor);
  70.     }
  71.     if (playList.style.highlightTexture!=null) {
  72.         playListStyle.onNormal.background = playList.style.highlightTexture;
  73.     } else {
  74.         playListStyle.onNormal.background = ColorToTexture2D(playList.style.highlightColor);
  75.     }
  76.     playListStyle.normal.textColor = playList.style.textColor;
  77.     playListStyle.onNormal.textColor = playList.style.textColor;
  78.     if (playList.style.background!=null) {
  79.         plTexture = playList.style.background;
  80.     } else {
  81.         plTexture = ColorToTexture2D(playList.style.backgroundColor);
  82.     }
  83. }
  84. //main code-----------------------------------------------------------------------------
  85. function Update() {
  86.     if (playMode!=3 && !audio.isPlaying && isPl) {
  87.         selectedOption++;
  88.     }
  89.     if (selectedOption>=tracks.length) {
  90.         selectedOption = 0;
  91.         if (playMode==0) {
  92.             isPl = false;
  93.         }
  94.     }
  95.     if (isPl && !audio.isPlaying) {
  96.         audio.Play();
  97.     }
  98. }
  99.  
  100. function MouseClick(position:Vector2) {
  101.     Debug.Log("works");
  102.     position.x += Screen.width/2;
  103.     position.y += Screen.height/2;
  104.     var tempRect = playListRect;
  105.     tempRect.x -= playList.margin.left;
  106.     tempRect.y -= playList.margin.top;
  107.     tempRect.width -= (playList.margin.left+playList.margin.right);
  108.     tempRect.height -= (playList.margin.top+playList.margin.bottom);
  109.     if (tempRect.Contains(position)) {
  110.         isPl = true;
  111.         if (audio.clip!=tracks[selectedOption]) audio.clip = tracks[selectedOption];
  112.         audio.Play();
  113.     }
  114. }
  115. //gui------------------------------------------------------------------------------------
  116. function OnGUI() {
  117.     GUI.Label(Rect(0,0,Screen.width,Screen.height),"",bgStyle);
  118.     PlayList();
  119. }
  120.  
  121. function PlayList() {
  122.     var isClick = (Event.current.type == EventType.MouseUp && Event.current.clickCount == 1);
  123.     playListRect = RectGetter(playList.rect,playList.anchor,playList.rectType);
  124.     var marg = playList.margin;
  125.     var st = GUIStyle();
  126.     st.normal.background = plTexture;
  127.     GUILayout.BeginArea(playListRect,st);
  128.     GUILayout.BeginVertical("");
  129.     GUILayout.Space(marg.top);
  130.     GUILayout.BeginHorizontal("");
  131.     GUILayout.Space(marg.left);
  132.     scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);
  133.     selectedOption = GUILayout.SelectionGrid(selectedOption, options, 1, playListStyle);
  134.     if(isClick)
  135.         MouseClick(Event.current.mousePosition);
  136.     GUILayout.EndScrollView();
  137.     GUILayout.Space(marg.right);
  138.     GUILayout.EndHorizontal();
  139.     GUILayout.Space(marg.bottom);
  140.     GUILayout.EndVertical();
  141.     GUILayout.EndArea();
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. function RectGetter(rect:Rect,type:MP_Anchor,rt:MP_RectType) {
  149.     if (rt.x == MP_RT.Percentage) {
  150.         rect.x = Screen.width*rect.x;
  151.     }
  152.     if (rt.y == MP_RT.Percentage) {
  153.         rect.y = Screen.height*rect.y;
  154.     }
  155.     if (rt.width == MP_RT.Percentage) {
  156.         rect.width = Screen.width*rect.width;
  157.     }
  158.     if (rt.height == MP_RT.Percentage) {
  159.         rect.height = Screen.height*rect.height;
  160.     }
  161.     if (type == MP_Anchor.TopRight || type == MP_Anchor.BottomRight) {
  162.         rect.x = Screen.width-rect.x;
  163.     }
  164.     if (type == MP_Anchor.BottomLeft || type == MP_Anchor.BottomRight) {
  165.         rect.y = Screen.height-rect.y;
  166.     }
  167.     return rect;
  168. }
  169.  
  170.  
  171. private static var C2T_textures = new Hashtable();
  172. static function ColorToTexture2D(color : Color) : Texture2D
  173. {
  174.     var colorHash : int = color.GetHashCode();
  175.     //Debug.Log(colorHash);
  176.     var texture : Texture2D = C2T_textures[colorHash];
  177.     if(texture) return texture;
  178.     texture = new Texture2D(1,1);
  179.     texture.SetPixel(0,0,color);
  180.     texture.Apply();
  181.     C2T_textures[colorHash] = texture;
  182.     return texture;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement