Songyan 发表于 2014-4-3 17:12

Unity 3D制作小地图,显示、隐藏小地图和背景音乐播放与暂停

using UnityEngine;
using System.Collections;

public class jietu:MonoBehaviour {
        public bool flag1 = true;
        public Texture[] MinMap =new Texture ;
        public GUISkin background;
        //public Texture biankuang;
        public Texture[] MMBtn=new Texture;
        public Texture playerTexture ;
        float cubePosX=0 ;
        float cubePosY=0 ;
        publicGameObject MyPlayer;
        public GameObject MyPlane;
        float planeWidth;
        private float planeX;
        private float planeZ;
        public bool flag2=false;
        public Texture[] texture=new Texture;
        void Start()
        {
                if (MyPlane == null)
                        return;

                planeWidth= MyPlane.GetComponent<MeshFilter>().mesh.bounds.size.x*MyPlane.transform.localScale.x;//因为plane是正方形,所以只取了宽
                planeX = MyPlane.transform.position.x;
                planeZ = MyPlane.transform.position.z;

//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/myMap.png", typeof(UnityEngine.Texture));
//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/biankuang.png", typeof(UnityEngine.Texture));
//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/hidden.png", typeof(UnityEngine.Texture));
//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/zhankai.png", typeof(UnityEngine.Texture));
//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/play.png", typeof(UnityEngine.Texture));
//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/jingyin.png", typeof(UnityEngine.Texture));
//                MinMap=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/lan wa.jpg", typeof(UnityEngine.Texture));
        }
        void OnGUI ()
        {
                //float btnW = 60.0f;
                GUI.skin = background;
                //float btnH = 40.0f;
                if (MinMap == null)
                        return;
                int i = 0;
                MMBtn = MinMap ;
                MMBtn = MinMap ;
                playerTexture = MinMap ;
                if (flag1) {
                        i++;
                        GUI.DrawTexture (new Rect (Screen.width - MinMap.width, 0, MinMap.width, MinMap.height), MinMap);
                        GUI.DrawTexture(new Rect(Screen.width - MinMap.width, 0,MinMap.width,MinMap.height),MinMap);
                        if(cubePosX>0&&cubePosY<MinMap.height){
                                GUI.DrawTexture (new Rect (cubePosX + Screen.width - MinMap.width, cubePosY, 10, 10), playerTexture);
                                }
                }
                //绘制小地图现实与隐藏按钮
                if(GUI.Button (new Rect(Screen.width - MinMap.width-MMBtn.width-10,0,25,25),MMBtn)){
                        flag1 = !flag1;       
                }
                int j = 0;
                texture = MinMap ;
                texture = MinMap ;
                if (flag2) {
                        j++;
                        audio.Pause ();
                }
                if (GUI.Button(new Rect(Screen.width-282, 25, 25, 25), texture)){
                        audio.Pause();
                        audio.Play();
                        flag2=!flag2;
                }


        }
       
        void Update()
        {
                if (MinMap == null)
                        return;
                if (MyPlane == null)
                        return;

                cubePosX = MinMap.width * (MyPlayer.transform.position.x-planeX + planeWidth/2) / planeWidth;   //+ MinMap.width / 2;//根据palyer在plane的比例关系,映射到对应地图位置。
                cubePosY = -(MinMap.height * (MyPlayer.transform.position.z-planeZ - planeWidth/2) / planeWidth);   //+ MinMap.height / 2;
        }

}

aaabbbsss12345 发表于 2014-4-25 13:48

好东西 顶顶顶

摞你命3000 发表于 2014-11-19 14:34

有实验过可行么?我是新手,效果图能贴一个来看看么

xshenmo 发表于 2014-12-28 11:13

先保存看看

chinabeater 发表于 2015-1-16 11:16

感谢楼主的无私分享

肥波正传 发表于 2017-2-11 10:54

好帖就是要顶

逆风如解意。 发表于 2017-2-11 10:33

顶顶多好

Clear。 发表于 2017-2-11 10:01

难得一见的好帖

zldarwin 发表于 2017-2-11 10:47

不错不错

逆风如解意。 发表于 2017-2-11 10:31

LZ真是人才
页: [1] 2 3
查看完整版本: Unity 3D制作小地图,显示、隐藏小地图和背景音乐播放与暂停