先看效果(主要实现点击按钮切换图片,未解锁按钮弹出提示,点击过后播放动画)
预备知识(单例模式,携程, Resources.Load加载资源的方式)
资源准备(底部按钮7个图标,1920*1080的图片7个)
1.场景搭建(创建4个空对象并修改名字,创建Resources文件夹并在其中创建4个子文件夹并把准备的资源放到里面去)
2.创建一个画布并将这两个空对象(LayerRoot,Bottom)拖到画布中,并设置位置
3.设置画布的缩放模式(设置缩放模式并调整分辨率为1920*1080)
4.制作界面(在LayerRoot中创建一个图片(Image)并修改名字为“界面1”,将准备的图片拖到图片的原图像中,然后设置图片的原生大小)
最后得到这样一张效果图
5.用同样的方法制作界面2-7,并将它们拖到Layers文件夹中做成预制体
6.给Bottom和LayerRoot添加RectTransform组件
7.删除多余的界面然后设置Bottom的锚点为底部(这样是为了适应不同分辨率的屏幕)
8.界面搭建
9.制作提示预制体并拖到Tips文件夹中
10.创建脚本文件夹并编写3个脚本(ClickText,TipsMage,UIManage)(控制点击的脚本,提示管理器,界面管理器)
TipsMage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TipsMage : MonoBehaviour
{
//单例模式
public static TipsMage Instance;
void Start()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public void ShowTips(string titel , string content)
{
//利用Resources.Load加载预制体
GameObject tips = Resources.Load("Tips/Tips") as GameObject;
//实例化预制体
GameObject tipobj = Instantiate(tips);
//设置tips的标题和内容
tipobj.transform.Find("Tips_box/titel").GetComponent<Text>().text = titel;
tipobj.transform.Find("Tips_box/content").GetComponent<Text>().text = content;
//找到装提示的容器
GameObject UiLayer= GameObject.Find("LayerRoot");
//将提示添加到容器中
tipobj.transform.SetParent(UiLayer.transform);
tipobj.transform.position= UiLayer.transform.position;
//一段时间后销毁提示
Destroy(tipobj, 2f);
}
}
UIManage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManage : MonoBehaviour
{
//单例模式
public static UIManage Instance;
//放置所有UI的根节点
private Transform _uiLayerRoot;
//存放已经打开过的UI
private List<GameObject> _openedUI = new List<GameObject>();
//上一个打开的UI
private string _lastOpenUIname;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
_uiLayerRoot=GameObject.Find("LayerRoot").transform;
_lastOpenUIname = "Layers/icom1";
_openedUI.Add(GameObject.Find("Layers/icom1"));
}
//打开单个
public void Openlayer(string layerName)
{
//查找之前是否打开过
GameObject openedLayer = _openedUI.Find(item => item.name == layerName);
if (openedLayer == null && layerName!="")
{
Closelayer(_lastOpenUIname);
GameObject newLayer = Resources.Load(layerName) as GameObject;
GameObject layerInstance = Instantiate(newLayer, _uiLayerRoot.position, Quaternion.identity);
_openedUI.Add(layerInstance);
layerInstance.name = layerName;
layerInstance.transform.SetParent(_uiLayerRoot);
_lastOpenUIname=layerName;
}
else if (openedLayer.activeSelf == false)
{
GameObject layer = _openedUI.Find(item => item.name == layerName);
if (layer != null)
{
layer.SetActive( true);
}
Closelayer(_lastOpenUIname);
_lastOpenUIname = layerName;
}
}
//关闭单个
public void Closelayer(string layerName)
{
GameObject layer = _openedUI.Find(item => item.name == layerName);
if (layer!= null)
{
layer.SetActive(false);
}
}
//关闭所有
public void CloseAll()
{
foreach (GameObject ui in _openedUI)
{
ui.SetActive(false);
}
}
}
ClickText
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ClickText : MonoBehaviour
{
// 显示的界面名字
public string OpenLayerName;
// 控制动画的携程
Coroutine JumpControllercont;
// 静态变量来跟踪当前正在播放动画的协程
private static ClickText currentAnimatedButton;
void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(() =>
{
if (OpenLayerName == "")
{
TipsMage.Instance.ShowTips("友好提示", "你还没有设置显示的界面");
}
else
{
// 如果有其他按钮正在播放动画,停止它的协程
if (currentAnimatedButton != null && currentAnimatedButton != this)
{
currentAnimatedButton.StopCoroutine(currentAnimatedButton.JumpControllercont);
currentAnimatedButton.gameObject.transform.Find("icom").GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
}
UIManage.Instance.Openlayer(OpenLayerName);
if (JumpControllercont == null || currentAnimatedButton != this)
{
JumpControllercont = StartCoroutine(JumpController());
// 设置当前播放动画的按钮为这个按钮
currentAnimatedButton = this;
}
}
});
//设置默认点击按钮
if (gameObject.name == "Option1")
{
gameObject.GetComponent<Button>().onClick.Invoke();
}
}
IEnumerator JumpController()
{
while (true)
{
gameObject.transform.Find("icom").GetComponent<RectTransform>().localPosition = new Vector3(0, 100, 0);
yield return new WaitForSeconds(0.5f);
gameObject.transform.Find("icom").GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
yield return new WaitForSeconds(0.5f);
}
}
}
11.先将提示管理器和界面管理器拖到对应的空对象上
12.给所有的白色方块先添加按钮组件再添加 刚刚编写的脚本
13.在这里设置你点击按钮后想要打开的界面(Layers(文件夹的名字)/界面的名字)
如果不设置点击名字那么就是弹出刚刚设置的提示框
基本信息代码里面都写了注释,资源包链接 GitHub - laozhupeiqia/Princess-Connect-