Unity3D中实现箭头指向目标点的效果(shader)

系列文章目录

Unity工具


文章目录


前言

大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。


提示:以下是本篇文章正文内容,下面案例可供参考

一、效果如下

Unity箭头指向目标点视频

二、制作步骤

2-1、制作shader

首先先生成shader文件

2-2、shader代码

c 复制代码
Shader "Custom/ZhiXiangJianTou"
{
   Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _ScrollYSpeed("Y Scroll Speed", Range(-20, 20)) = 2
    }
        SubShader
        {
            Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
            LOD 100
            //双面渲染
            Cull Off
            //Alpha混合
            Blend SrcAlpha OneMinusSrcAlpha

            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"

                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };

                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    float4 vertex : SV_POSITION;
                };

                sampler2D _MainTex;
                float4 _MainTex_ST;
                fixed _ScrollYSpeed;

                v2f vert(appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                    return o;
                }

                fixed4 frag(v2f i) : SV_Target
                {
                    fixed2 uv = i.uv;
                    uv.y += _ScrollYSpeed * _Time;
                    fixed4 col = tex2D(_MainTex, uv);
                    return col;
                }
                ENDCG
            }
        }
}

2-3、制作材质球


所需要的贴图

2-4、新建Quad

新建一个Quad的对象,给他换上刚刚的材质球

做成预制体一会用

2-5、制作预制体

1.拖到Project自己创的文件夹下面

2.如箭头就是刚刚生层的预制体,留着一会用

2-6 、实现代码

预制体一拖,路径点一拖,运行即可看到效果

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZhiXiangJianTou : MonoBehaviour
{
    public MeshRenderer meshRenderer;//箭头3D对象Quad 预制体
    public List<Transform> points = new List<Transform>();//路径点
    private List<MeshRenderer> lines = new List<MeshRenderer>();//显示的路径

    public float xscale = 1f;//缩放比例
    public float yscale = 1f;

    void Start()
    {
        //箭头宽度缩放值
        xscale = meshRenderer.transform.localScale.x;
        //箭头长度缩放值
        yscale = meshRenderer.transform.localScale.y;
    }

    //画路径
    public void DrawPath()
    {
        if (points == null || points.Count <= 1)
            return;
        for (int i = 0; i < points.Count - 1; i++)
        {
            DrawLine(points[i].position, points[i + 1].position, i);
        }
    }

    //画路径 参数为路径点数组
    public void DrawPath(Vector3[] points)
    {
        if (points == null || points.Length <= 1)
            return;
        for (int i = 0; i < points.Length - 1; i++)
        {
            DrawLine(points[i], points[i + 1], i);
        }
    }

    //隐藏路径
    public void HidePath()
    {
        for (int i = 0; i < lines.Count; i++)
            lines[i].gameObject.SetActive(false);
    }

    //画路径
    private void DrawLine(Vector3 start, Vector3 end, int index)
    {
        Debug.Log(transform.gameObject.name);
        MeshRenderer mr;
        if (index >= lines.Count)
        {
            mr = Instantiate(meshRenderer);
            lines.Add(mr);
        }
        else
        {
            mr = lines[index];
        }

        var tran = mr.transform;
        var length = Vector3.Distance(start, end);
        tran.localScale = new Vector3(xscale, length, 1);
        tran.position = (start + end) / 2;
        //指向end
        tran.LookAt(end);
        //旋转偏移
        tran.Rotate(90, 0, 0);
        mr.material.mainTextureScale = new Vector2(1, length * yscale);
        mr.gameObject.SetActive(true);
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(20, 40, 80, 20), "显示路径"))
        {
            DrawPath();
        }
        if (GUI.Button(new Rect(20, 80, 80, 20), "隐藏路径"))
        {
            HidePath();
        }
    }
}

2-7、设置Quad到脚本

2-8、路径设置如下

三、说明

这个是已经计算过根据距离算shader的间隔,不论两个点的间隔多远,shader的渲染间隔是一样的,所以不用担心每个点的间隔不一样了,毫无影响,代码直接用就好了

四、运行程序

Unity箭头指向目标点视频


总结

你的点赞就是对博主的支持,有问题记得留言:

不定时更新Unity开发技巧,觉得有用记得一键三连哦。

相关推荐
Dr.勿忘2 小时前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
存储服务专家StorageExpert3 小时前
答疑解惑:如何监控EMC unity存储系统磁盘重构rebuild进度
运维·unity·存储维护·emc存储
Petrichorzncu5 小时前
Games104——游戏引擎Gameplay玩法系统:基础AI
游戏引擎
追逐梦想永不停9 小时前
Unity实现按键设置功能代码
unity
我命由我123451 天前
游戏引擎 Unity - Unity 下载与安装
c语言·开发语言·c++·后端·unity·c#·游戏引擎
车载诊断技术1 天前
车载软件架构 --- 基于AUTOSAR软件架构的ECU开发流程小白篇
网络·unity·架构·汽车·电子电器框架·车载充电器(obc)
我命由我123451 天前
游戏引擎 Unity - Unity 启动(下载 Unity Editor、生成 Unity Personal Edition 许可证)
c语言·c++·后端·unity·c#·游戏引擎·ue4
我命由我123451 天前
游戏开发领域 - 游戏引擎 UE 与 Unity
开发语言·c++·unity·c#·游戏引擎·unreal engine·unreal engine 4
一个一定要撑住的学习者1 天前
Day29(补)-【AI思考】-精准突围策略——从“时间贫困“到“效率自由“的逆袭方案
人工智能·unity·游戏引擎
qq_428639612 天前
虚幻基础08:组件接口
游戏引擎·虚幻