Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum MP_Anchor {TopLeft, TopRight, BottomLeft, BottomRight};
- enum MP_RT { Pixel, Percentage };
- class MP_RectType {
- var x : MP_RT = MP_RT.Pixel;
- var y : MP_RT = MP_RT.Pixel;
- var width : MP_RT = MP_RT.Pixel;
- var height : MP_RT = MP_RT.Pixel;
- }
- class MP_Style {
- var background : Texture2D;
- var backgroundColor : Color = Color.clear;
- var normalTexture : Texture2D;
- var normalColor : Color = Color.clear;
- var highlightTexture : Texture2D;
- var highlightColor : Color = Color.white;
- var textColor : Color = Color.black;
- }
- class MP_Marg {
- var top : float = 0;
- var left : float = 0;
- var right : float = 0;
- var bottom : float = 0;
- }
- class MP_PlayList {
- var anchor : MP_Anchor = MP_Anchor.TopLeft;
- var rect : Rect = Rect(0,0,300,300);
- var rectType : MP_RectType;
- var margin : MP_Marg;
- var style : MP_Style;
- }
- var tracks : AudioClip[];
- var background : Texture2D;
- var playList : MP_PlayList;
- //------------------------------------------------------------
- private var options = ['A','B','C','D','E','F'];
- private var selectedOption = 0;
- private var scrollPosition : Vector2 = new Vector2(0,0);
- private var playListStyle : GUIStyle = GUIStyle();
- private var bgStyle : GUIStyle = GUIStyle();
- private var playListRect : Rect;
- private var plTexture : Texture2D;
- private var isPl : boolean = false;
- private var playMode : int = 0;
- private var oldScreenSize : Vector2;
- //start--------------------------------------------------------------------------------
- function Start() {
- OtherStart();
- if (audio==null) gameObject.AddComponent(AudioSource);
- audio.playOnAwake = false;
- audio.clip = tracks[0];
- options = new String[tracks.length];
- for (i = 0;i<tracks.length;i++) {
- options[i] = tracks[i].name;
- }
- PlayListStart();
- }
- function OtherStart() {
- bgStyle.normal.background = background;
- }
- function PlayListStart() {
- if (playList.style.normalTexture!=null) {
- playListStyle.normal.background = playList.style.normalTexture;
- } else {
- playListStyle.normal.background = ColorToTexture2D(playList.style.normalColor);
- }
- if (playList.style.highlightTexture!=null) {
- playListStyle.onNormal.background = playList.style.highlightTexture;
- } else {
- playListStyle.onNormal.background = ColorToTexture2D(playList.style.highlightColor);
- }
- playListStyle.normal.textColor = playList.style.textColor;
- playListStyle.onNormal.textColor = playList.style.textColor;
- if (playList.style.background!=null) {
- plTexture = playList.style.background;
- } else {
- plTexture = ColorToTexture2D(playList.style.backgroundColor);
- }
- }
- //main code-----------------------------------------------------------------------------
- function Update() {
- if (playMode!=3 && !audio.isPlaying && isPl) {
- selectedOption++;
- }
- if (selectedOption>=tracks.length) {
- selectedOption = 0;
- if (playMode==0) {
- isPl = false;
- }
- }
- if (isPl && !audio.isPlaying) {
- audio.Play();
- }
- }
- function MouseClick(position:Vector2) {
- Debug.Log("works");
- position.x += Screen.width/2;
- position.y += Screen.height/2;
- var tempRect = playListRect;
- tempRect.x -= playList.margin.left;
- tempRect.y -= playList.margin.top;
- tempRect.width -= (playList.margin.left+playList.margin.right);
- tempRect.height -= (playList.margin.top+playList.margin.bottom);
- if (tempRect.Contains(position)) {
- isPl = true;
- if (audio.clip!=tracks[selectedOption]) audio.clip = tracks[selectedOption];
- audio.Play();
- }
- }
- //gui------------------------------------------------------------------------------------
- function OnGUI() {
- GUI.Label(Rect(0,0,Screen.width,Screen.height),"",bgStyle);
- PlayList();
- }
- function PlayList() {
- var isClick = (Event.current.type == EventType.MouseUp && Event.current.clickCount == 1);
- playListRect = RectGetter(playList.rect,playList.anchor,playList.rectType);
- var marg = playList.margin;
- var st = GUIStyle();
- st.normal.background = plTexture;
- GUILayout.BeginArea(playListRect,st);
- GUILayout.BeginVertical("");
- GUILayout.Space(marg.top);
- GUILayout.BeginHorizontal("");
- GUILayout.Space(marg.left);
- scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);
- selectedOption = GUILayout.SelectionGrid(selectedOption, options, 1, playListStyle);
- if(isClick)
- MouseClick(Event.current.mousePosition);
- GUILayout.EndScrollView();
- GUILayout.Space(marg.right);
- GUILayout.EndHorizontal();
- GUILayout.Space(marg.bottom);
- GUILayout.EndVertical();
- GUILayout.EndArea();
- }
- function RectGetter(rect:Rect,type:MP_Anchor,rt:MP_RectType) {
- if (rt.x == MP_RT.Percentage) {
- rect.x = Screen.width*rect.x;
- }
- if (rt.y == MP_RT.Percentage) {
- rect.y = Screen.height*rect.y;
- }
- if (rt.width == MP_RT.Percentage) {
- rect.width = Screen.width*rect.width;
- }
- if (rt.height == MP_RT.Percentage) {
- rect.height = Screen.height*rect.height;
- }
- if (type == MP_Anchor.TopRight || type == MP_Anchor.BottomRight) {
- rect.x = Screen.width-rect.x;
- }
- if (type == MP_Anchor.BottomLeft || type == MP_Anchor.BottomRight) {
- rect.y = Screen.height-rect.y;
- }
- return rect;
- }
- private static var C2T_textures = new Hashtable();
- static function ColorToTexture2D(color : Color) : Texture2D
- {
- var colorHash : int = color.GetHashCode();
- //Debug.Log(colorHash);
- var texture : Texture2D = C2T_textures[colorHash];
- if(texture) return texture;
- texture = new Texture2D(1,1);
- texture.SetPixel(0,0,color);
- texture.Apply();
- C2T_textures[colorHash] = texture;
- return texture;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement