UGUI制作狼爪效果Demo要点(比较菜,不会Shader,以后再改进)

最终效果图:

最终效果图

方法步骤:
1.P一张狼爪图(其余部分为透明,保存为.jpg或者.png,大小1M以内);
2.导入一个汽车模型
3.导入狼爪图
这里写图片描述

这里写图片描述
(Canvas的Render Mode设置为Screen Space - Camera,将Camera拖入Render Camera中)
这里写图片描述

3.直接上代码
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;

public class RotateTexture : MonoBehaviour {
    [SerializeField]
    private Image wolfClaw;
    [SerializeField]
    private Camera renderCamera;//渲染相机

    public float wolfClawHalfWidth, wolfClawHalfHeight;

    public static RotateTexture instance;
    private void Awake()
    {
        instance = this;

        wolfClawHalfWidth = wolfClaw.rectTransform.rect.width / 2;
        wolfClawHalfHeight = wolfClaw.rectTransform.rect.height / 2;
    }
    // Use this for initialization
    void Start () {
        ImageRotate(0,new Vector2(-wolfClawHalfWidth, -wolfClawHalfHeight));//程序开始的时候运行一次,让renderCamera中的画面不出现在屏幕上
    }
    public Color[] ImageRotate(float angle,Vector2 position)//旋转,位置
    {
        wolfClaw.rectTransform.localRotation = Quaternion.Euler(new Vector3(0, 0, angle));
        wolfClaw.rectTransform.anchoredPosition = position; 
        return CameraShot(renderCamera);
    }
    Color[] CameraShot(Camera camera)//截图
    {
        //new一个RenderTexture接收相机画面
        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 0);
        camera.targetTexture = rt;
        camera.Render();

        //激活rt,从中读取像素
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32,false);
        screenShot.ReadPixels(new Rect(0,0,Screen.width, Screen.height), 0, 0);
        screenShot.Apply();//这个时候是针对renderCamera截图

        //File.WriteAllBytes(Application.dataPath + "/screenShot.png", screenShot.EncodeToPNG());
        ////重置相关参数
        //camera.targetTexture = null;
        //RenderTexture.active = null;
        Color[] cs = screenShot.GetPixels();

        //透明与不透明互相转变
        for (uint i = 0; i < cs.Length; i++)
        {
            if (cs[i].a == 0)
            {
                cs[i].a = 1f;
            }
            else
            {
                cs[i].a = 0f;
            }
        }
        screenShot.SetPixels(cs);
        screenShot.Apply();

        File.WriteAllBytes(Application.dataPath + "/screenShot.png", screenShot.EncodeToPNG());

        return cs;
    }
    // Update is called once per frame
    void Update () {

    }
}



using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
//using System.Drawing;

public class TearScreen : MonoBehaviour
{
    [SerializeField]
    private UnityEngine.UI.Image curtain;//幕布

    private string curtainTexturePath;//幕布图片路径

    private Texture2D curtainTexture;//幕布图片

    public int growSpeed = 5,maxPixel = 50;//粗细变化速度,像素最大宽度

    private float checkRate = 0.5f;//检测的间隔秒数
    // Use this for initialization
    void Start()
    {
        curtainTexturePath = @Application.dataPath + @"/Texture/" + "Curtain.png";
        curtainTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);

        curtainTexture = LoadTexture(curtainTexturePath);//加载幕布图片

        ResizeTexture(curtainTexture, Screen.width, Screen.height);//适配屏幕大小

        curtain.sprite = Sprite.Create(curtainTexture, new Rect(0, 0,
            curtainTexture.width, curtainTexture.height), new Vector2(0.5f, 0.5f));//转换成Sprite,并赋值给幕布Image

        //StartCoroutine("PerFrame");
    }
    Vector2 begin = Vector2.zero, end = Vector2.zero;//开始点,终止点
    //No.1 弄出狼爪效果
    private void FixedUpdate()
    {
        if (Input.GetMouseButtonDown(0))
        {
            begin = V3ToV2(Input.mousePosition);
            Debug.Log(begin.x + "**Down**" + begin.y);
        }
        if (Input.GetMouseButtonUp(0))
        {
            end = V3ToV2(Input.mousePosition);
            Debug.Log(end.x + "**up**" + end.y);

            if ((begin - end) != Vector2.zero)
            {
                float angle = Vector2.Angle(end - begin, Vector2.right);

                if (end.y < begin.y)//第三、四象限
                {
                    angle = -angle;//三四象限的角度需要调整
                }
                //Debug.Log("2.angle ::::" + angle);
                OnWolfClaw(angle, begin);
            }
        }
    }
    void OnWolfClaw(float angle, Vector2 begin)
    {
        Color[] wolfClawColors = RotateTexture.instance.ImageRotate(angle, begin);
        Color[] curtainColors = curtainTexture.GetPixels();

        for (uint i = 0; i < wolfClawColors.Length; i++)
        {
            if (wolfClawColors[i].a != 0)
            {
                wolfClawColors[i] = curtainColors[i];//像素替换
            }
        }
        curtainTexture.SetPixels(wolfClawColors);//合成
        curtainTexture.Apply();

        curtain.overrideSprite = Sprite.Create(curtainTexture, new Rect(0, 0,
   curtainTexture.width, curtainTexture.height), new Vector2(0.5f, 0.5f));
    }
    Texture2D LoadTexture(string path)
    {
        Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        fs.Seek(0, SeekOrigin.Begin);

        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);

        fs.Close();
        fs.Dispose();
        fs = null;

        tex.LoadImage(bytes);
        tex.Apply();

        return tex;
    }
    void ResizeTexture(Texture2D tex, int width, int height)
    {
        UnityEngine.Color[] cs = tex.GetPixels();
        tex.Resize(width, height, TextureFormat.ARGB32, false);
        tex.SetPixels(cs);
        tex.Apply();//别忘记每次Apply!!
    }
    Vector2 V3ToV2(Vector3 v3)
    {
        return new Vector2(GetPositiveNum(v3.x), GetPositiveNum(v3.y));
    }
    float GetPositiveNum(float num)
    {
        if (num < 0)
        {
            return -num;
        }
        return num;
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值