找回密码
 立即注册
查看: 4800|回复: 76

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

[复制链接]
发表于 2014-4-3 17:12 | 显示全部楼层 |阅读模式
using UnityEngine;
using System.Collections;

public class jietu:MonoBehaviour {
        public bool flag1 = true  ;
        public Texture[] MinMap =new Texture[7] ;
        public GUISkin background;
        //public Texture biankuang  ;
        public Texture[] MMBtn=new Texture[2];
        public Texture playerTexture ;
        float cubePosX=0 ;
        float cubePosY=0 ;
        public  GameObject MyPlayer  ;
        public GameObject MyPlane;
        float planeWidth;
        private float planeX;
        private float planeZ;
        public bool flag2=false;
        public Texture[] texture=new Texture[2];
        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[0]=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/myMap.png", typeof(UnityEngine.Texture));
//                MinMap[1]=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/biankuang.png", typeof(UnityEngine.Texture));
//                MinMap[2]=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/hidden.png", typeof(UnityEngine.Texture));
//                MinMap[3]=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/zhankai.png", typeof(UnityEngine.Texture));
//                MinMap[4]=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/play.png", typeof(UnityEngine.Texture));
//                MinMap[5]=(Texture)Resources.LoadAssetAtPath("Assets/Materials/gulaochengbao/Texture/jingyin.png", typeof(UnityEngine.Texture));
//                MinMap[6]=(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 [0] = MinMap [3];
                MMBtn [1] = MinMap [2];
                playerTexture = MinMap [6];
                if (flag1) {
                        i++;
                        GUI.DrawTexture (new Rect (Screen.width - MinMap[0].width, 0, MinMap[0].width, MinMap[0].height), MinMap[0]);
                        GUI.DrawTexture(new Rect(Screen.width - MinMap[1].width, 0,MinMap[1].width,MinMap[1].height),MinMap[1]);
                        if(cubePosX>0&&cubePosY<MinMap[0].height){
                                GUI.DrawTexture (new Rect (cubePosX + Screen.width - MinMap[0].width, cubePosY, 10, 10), playerTexture);
                                }
                }
                //绘制小地图现实与隐藏按钮
                if(GUI.Button (new Rect(Screen.width - MinMap[1].width-MMBtn[i].width-10,0,25,25),MMBtn[i])){
                        flag1 = !flag1;       
                }
                int j = 0;
                texture [0] = MinMap [4];
                texture [1] = MinMap [5];
                if (flag2) {
                        j++;
                        audio.Pause ();
                }
                if (GUI.Button(new Rect(Screen.width-282, 25, 25, 25), texture[j])){
                        audio.Pause();
                        audio.Play();
                        flag2=!flag2;
                }


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

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

}

评分

参与人数 4鲜花 +4 收起 理由
1287537617aa + 1
aa1287537617 + 1
失却 + 1
npshj + 1 赞一个!

查看全部评分

发表于 2014-4-25 13:48 | 显示全部楼层
好东西 顶顶顶
发表于 2014-11-19 14:34 | 显示全部楼层
有实验过可行么?我是新手,效果图能贴一个来看看么
发表于 2014-12-28 11:13 | 显示全部楼层
先保存看看
发表于 2015-1-16 11:16 | 显示全部楼层
感谢楼主的无私分享
发表于 2017-2-11 10:54 | 显示全部楼层
好帖就是要顶
发表于 2017-2-11 10:33 | 显示全部楼层
顶顶多好
发表于 2017-2-11 10:01 | 显示全部楼层
难得一见的好帖
发表于 2017-2-11 10:47 | 显示全部楼层
不错不错
发表于 2017-2-11 10:31 | 显示全部楼层
LZ真是人才
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-15 07:14 , Processed in 0.110911 second(s), 31 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表