1. 首先是书本的本体也就是每一页一个文本
因为是基础向所以一行行说
a.
创建一个Book类(包含一个string 就够了,想要其他功能也可以自己加上)
[TextAreaAttribute(15, 20)]
是用来改变检查器的某些尺寸 这里是改变 检查器里面 text 编辑框的大小(为了看起来方便一点)
b. 新建一个类里面要包含
刚才那个书的类 的数组 , 弄个下标索引 为了方便左半书本做一个按钮 ,右半书本做一个按钮,书本的动画 ,文本。
c.获取组件
d. 这个脚本要实现的是按左边的按钮往左面翻书, 按右面的按钮往右翻书并且 下标 加 或 减
先来一个一直根据下标检索 文本框内容
e.
我这里的脚本是用来实现我自己需要的功能,加不加无所谓
最后代码如下图。
我这里的脚本是用来实现我自己需要的功能,加不加无所谓
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; [System.Serializable] public class Book { [TextAreaAttribute(15, 20)] public string currentpagecontext; } public class DoingFanshu : MonoBehaviour { public Book[] ThisBook; public int currentPage = 0; // Start is called before the first frame update private Animator BookAnimator; private Button BookTriggerButton; private Button BookTriggerButtonleft; public Text BookText; // private bool click = false; void Start() { BookAnimator = GetComponent<Animator>(); BookTriggerButton = GameObject.FindGameObjectWithTag("BOOKR").GetComponent<Button>(); BookTriggerButtonleft = GameObject.FindGameObjectWithTag("BOOKL").GetComponent<Button>(); BookTriggerButton.onClick.AddListener(() => buttonjihuo()); BookTriggerButtonleft.onClick.AddListener(() => buttonjihuo_zuo()); } // Update is called once per frame void Update() { BookText.text = ThisBook[currentPage].currentpagecontext.ToString(); //if (Input.GetMouseButtonDown(0) && !click) //{ // BookAnimator.SetBool("Shut", true); //} if(currentPage == 8) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); } } private void buttonjihuo() { if (BookAnimator.GetBool("Open")) { BookAnimator.SetTrigger("FanYe"); BookAnimator.SetBool("Idle2", true); if (currentPage < ThisBook.Length - 1) { currentPage++; } // click = true; // BookAnimator.SetBool("Open", false); } } private void buttonjihuo_zuo() { if (BookAnimator.GetBool("Open")) { BookAnimator.SetTrigger("FanYe_zuo"); BookAnimator.SetBool("Idle2", true); if (currentPage >0) { currentPage--; } // click = true; // BookAnimator.SetBool("Open", false); } } }
2.下面要实现的是 点击空地 合上书本并且收回
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
// 这个脚本用于点击空白区域隐藏物体的功能
public class Blocker : MonoBehaviour
{
// Canvas通过它接受点击事件
public GraphicRaycaster RaycastInCanvas;
public Image Book;
// 如果它之下还有多个嵌套的物体,需要把它们拖入此List中,以防止误判
private Animator BookAnami;
public List<Blocker> blockers;
EventSystem eventSystem;
// 为了避面判断下级时 多执行一次回调方法
bool state;
void Start()
{
// 获取当前的EventSystem组件
eventSystem = EventSystem.current;
// 检查RaycastInCanvas是否为空,如果为空,就从父物体中获取GraphicRaycaster组件
if (RaycastInCanvas == null)
RaycastInCanvas = GetComponentInParent<GraphicRaycaster>();
// RegisterEvent(Die);
BookAnami = Book.GetComponent<Animator>();
}
private void Update()
{
// 检测鼠标左键的按下和抬起事件
if (Input.GetMouseButtonDown(0)&&BookAnami.GetBool("Open"))
{
// 如果按下,就把state设为true
state = true;
}
if (Input.GetMouseButtonUp(0)&&BookAnami.GetBool("Open"))
{
// 如果抬起,就判断state是否为true,如果是,就调用CheckGuiRaycastObjects方法
// Debug.Log(CheckGuiRaycastObjects());
if (state)
{
// Debug.Log(CheckGuiRaycastObjects());
if (CheckGuiRaycastObjects())
{
Die();
}
}
}
}
public void Die()
{
if (BookAnami.GetBool("Idle2"))
{
BookAnami.SetTrigger("Shut");
BookAnami.SetBool("Open", false);
}
}
// 注册点击空白区域时的事件(比如 隐藏 干掉 需自己传入执行方法)
//public void RegisterEvent(Callback callback)
//{
// this.callback = callback;
//}
// 是否点击在空白区域
public bool CheckGuiRaycastObjects()
{
// 当执行了一次之后 本次鼠标点击就不再执行此方法
state = false;
// 创建一个PointerEventData对象,用于存储鼠标的位置信息
PointerEventData eventData = new PointerEventData(eventSystem);
eventData.pressPosition = Input.mousePosition;
eventData.position = Input.mousePosition;
// 创建一个RaycastResult类型的列表,用于存储射线检测到的UI元素
List<RaycastResult> list = new List<RaycastResult>();
// 调用RaycastInCanvas的Raycast方法,传入eventData和list参数,获取到所有鼠标射线检测到的UI元素
RaycastInCanvas.Raycast(eventData, list);
if (list.Count!=0)
{
// Debug.Log(list[0]);
}
// 判断list是否为空,如果不为空,就遍历list中的每个元素
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
// 获取每个元素的GameObject,并判断它们是否有Blocker组件或者它们的父物体是否有Blocker组件,如果有,就返回false,表示点击在物体上或其他Blocker上
GameObject go = list[i].gameObject;
// Debug.Log(go.GetComponentInParent<Blocker>() != null);
if (go.GetComponentInParent<Blocker>() != null)
{
return false;
}
}
}
// 判断blockers是否为空,如果不为空,就遍历blockers中的每个元素
if (blockers != null)
{
foreach (var item in blockers)
{
// 判断每个元素是否激活并且没有点击在空白区域上,如果是,就返回false
if (item.gameObject.activeSelf && !item.CheckGuiRaycastObjects())
{
return false;
}
}
}
// 如果以上都没有返回false,就说明点击在空白区域上了,就调用callback变量存储的回调函数,并返回true
return true;
}
}
因为我具体每行都干嘛的已经写再注释里了 ,就说点其他的,防止小萌新不懂
那个射线系统就相当与 你的摄像机,向你的鼠标位置 发射一条射线 ,穿透所有的UI元素
而你拥有 接受器的就可以接收到
这样就能实现按空地就收回了
翻书效果