赞
踩
提示:素材来源网络,侵权必删
角色与UI混合显示:在角色信息界面显示3D角色
实现角色旋转触控:可控制其旋转
提示:以下是本篇文章正文内容,下面案例可供参考
示例:
准备一个3D人物;
给人物的Layer设置一个层级Player;
示例:
给Camera的Culling Mask设置为前面设置的人物Player层
using System; using UnityEngine; using UnityEngine.EventSystems; namespace HKZ { public class PEListener : MonoBehaviour, IPointerDownHandler, IDragHandler { public Action<PointerEventData> onClickDown; public Action<PointerEventData> onDrag; /// <summary> /// 拖拽 /// </summary> /// <param name="eventData"></param> public void OnDrag(PointerEventData eventData) { if (onDrag != null) { onDrag(eventData); } } /// <summary> /// 按下 /// </summary> /// <param name="eventData"></param> public void OnPointerDown(PointerEventData eventData) { if (onClickDown != null) { onClickDown(eventData); } } } }
using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace HKZ { public class Test : MonoBehaviour { public RawImage imgChar; public Transform player; private Vector2 startPos; private Transform charCamTrsns; private void Awake() { gameObject.SetActive(false);//隐藏信息面板 //设置人物信息面板相机 if (charCamTrsns != null) { charCamTrsns.gameObject.SetActive(false); } RegTouchEvts(); } //按下和拖动 private void RegTouchEvts() { OnclickDown(imgChar.gameObject, (PointerEventData evt) => { startPos = evt.position;//当前点击位置 SetStartRotate();//记录开始角度 }); OnDrag(imgChar.gameObject, (PointerEventData evt) => { float rotate = -(evt.position.x - startPos.x) * 0.4f;//每次托拽间隔距离 SetPlayerRotate(rotate);//旋转 }); } //关闭页面 public void ClickCloseBtn() { gameObject.SetActive(false); } //打开页面 public void ClickOpenBtn() { if (charCamTrsns == null) { charCamTrsns = GameObject.FindGameObjectWithTag("CharShowCam").transform; } //设置人物展示相机相对位置 charCamTrsns.localPosition = player.transform.position + player.transform.forward * 3.8f + new Vector3(0, 1.2f, 0); charCamTrsns.localEulerAngles = new Vector3(0, 180 + player.transform.localEulerAngles.y, 0); charCamTrsns.localScale = Vector3.one; charCamTrsns.gameObject.SetActive(true); gameObject.SetActive(true); } private float startRotate = 0; //记录当前人物位置 private void SetStartRotate() { startRotate = player.transform.localEulerAngles.y; } //拖动后调用使其旋转 private void SetPlayerRotate(float rotate) { player.transform.localEulerAngles = new Vector3(0, startRotate + rotate, 0); } #region ClickEvts private T GetOrAddComponent<T>(GameObject go) where T : Component { T t = go.GetComponent<T>(); if (t == null) { t = go.AddComponent<T>(); } return t; } private void OnclickDown(GameObject go, Action<PointerEventData> cb) { PEListener listener = GetOrAddComponent<PEListener>(go); listener.onClickDown = cb; } private void OnDrag(GameObject go, Action<PointerEventData> cb) { PEListener listener = GetOrAddComponent<PEListener>(go); listener.onDrag = cb; } #endregion } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。