提示:文章有错误的地方,还望诸位大神不吝指教!
文章目录
前言
仅适用于VRAR项目!
需求:
人物说话时:头旁边出现说话内容,且根据内容长短,以打字效果的方式,进行向下自动扩充,并且UI复用,可以多人物使用。
功能:
1.永远朝向相机
2.打字效果
3.UI不会被模型遮挡
4.切换位置
5.文字自动根据内容扩充
效果:
一、UI搭建
1.创建基节点
创建空物体作为UI的父节点,改名字:UIDialog,因为是3dUI,大小和高度根据自己的情况调节即可
2.创建Canvas
因为是VR项目,所以就用XR创建Canvas:在Hierarchy面板中右击鼠标–>XR–>UICanvas
3.添加永远看向相机代码
using UnityEngine;
public class LookatPlayer : MonoBehaviour
{
Camera MainCamera;
void Awake()
{
MainCamera = Camera.main;
if (MainCamera == null)
{
Debug.LogError("没有找到MainCamera");
return;
}
}
void Update()
{
this.transform.LookAt(MainCamera.transform);
// this.transform.forward = Vector3.forward;
this.transform.localRotation = Quaternion.Euler(new Vector3(0, this.transform.localRotation.eulerAngles.y, 0));
}
}
4.创建UI背景,设置相关操作
1.锚点设置
把Pivot的Y改为:1,即可。作业:扩张时会向下,扩展。
2.添加组件:Vertical Layout Group、Content Size Fitter
Vertical Layout Group:竖向排列 (横向排列:Horizontal Layout Group)
Content Size Fitter:根据子物体内容多少扩展(可根据需求设置:横向或者竖向,如下图)
3.添加文本
我选择添加旧版文本:Text (Legacy),并没有使用Text (TMP),原因是后面要做一个UI不被模型遮挡的效果。
但是TextMeshPro - Text (UI)组件效果更好
二、UI不被模型遮挡
如果UI设置跟随,或者当前位置模型太多,会出现UI被模型挡住,看不全UI面板的内容。
所有Text、Image都需要指定该shader
效果图:
使用Shader修改渲染层级
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
Shader "Custom/UIForground"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
//ZTest [unity_GUIZTestMode]
ZTest Always
Blend One OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
half4 mask : TEXCOORD2;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
float4 _MainTex_ST;
float _UIMaskSoftnessX;
float _UIMaskSoftnessY;
v2f vert(appdata_t v)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
float4 vPosition = UnityObjectToClipPos(v.vertex);
OUT.worldPosition = v.vertex;
OUT.vertex = vPosition;
float2 pixelSize = vPosition.w;
pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
float2 maskUV = (v.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
OUT.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
OUT.mask = half4(v.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_UIMaskSoftnessX, _UIMaskSoftnessY) + abs(pixelSize.xy)));
OUT.color = v.color * _Color;
return OUT;
}
fixed4 frag(v2f IN) : SV_Target
{
half4 color = IN.color * (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);
#ifdef UNITY_UI_CLIP_RECT
half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
color.a *= m.x * m.y;
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
color.rgb *= color.a;
return color;
}
ENDCG
}
}
}
三、打字效果代码
using System.Collections;
using System.Collections.Generic;
using Twq;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 对话框(3DUI,可换到任意人物头部位置)
/// </summary>
public class UICS_Dialog : SingletonMono<UICS_Dialog>
{
public GameObject Mask;
float Speed = 10;
public Text textLabel;
void Start()
{
// GetText("作业负责人:进入场景后,弹出界面对任务进行一个介绍,可以视频、文字,点击开始");
}
/// <summary>
/// 获得文字
/// </summary>
/// <param name="textToType"></param>
public void GetText(string text_)
{
StartCoroutine(WriteText(text_));
}
/// <summary>
/// 写出来
/// </summary>
/// <param name="textToType"></param>
/// <returns></returns>
IEnumerator WriteText(string text_)
{
float t = 0;//经过的时间
int charIndex = 0;//字符串索引值
while (charIndex < text_.Length)
{
t += Time.deltaTime * Speed;//简单计时器赋值给t
charIndex = Mathf.FloorToInt(t);//把t转为int类型赋值给charIndex
charIndex = Mathf.Clamp(charIndex, 0, text_.Length);
textLabel.text = text_.Substring(0, charIndex);
yield return null;
}
textLabel.text = text_;
}
/// <summary>
/// 设置UI
/// </summary>
/// <param name="bo">UI显隐开关</param>
/// <param name="parentTran">父节点</param>
/// <param name="textStr">文字内容</param>
public void SetState(bool bo, Transform parentTran, string textStr)
{
this.transform.SetParent(parentTran);//设置父节点
this.transform.position = new Vector3(0,1.345f,0);//设置固定高度
this.transform.Rotate(new Vector3(0,0,0)); //设置旋转
Mask.SetActive(bo);//设置UI显隐
if (bo)
{
GetText(textStr);
}
}
}
总结
好记性不如烂笔头!