首页 / UNITY / Unity一个弹痕的简单实现方法
Unity一个弹痕的简单实现方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Unity一个弹痕的简单实现方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1979字,纯文字阅读大概需要3分钟。
内容图文
之前知道一个方法比较复杂就是取出贴图,类似于从上到下从左到右的去遍历一张图,去除像素点改变像素点。今天在选丞大佬那看到下面这个方法,觉得十分简单,原理应该是相同的吧。
官方文档:
https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html
附上中文版:
http://www.manew.com/youxizz/2393.html
新建一个脚本把上面链接中的代码复制进去,记得改下脚本名。将脚本挂在场景主相机上面:
在场景中随便搞个物体 组件如图:记得Mesh Collider 的Convex 不要勾选 PS:Unity省点的Convex解释:
(这个Convex 没太懂 知乎说也就是效率什么什么的。。https://www.zhihu.com/question/40575282)
接下来还要注意下这种贴图的设置 是否可写:
设置完了Apply.运行游戏就可以了:
只有草羊看看就画了一个草羊。关闭游戏后也可以看见贴图文件改变了:
以上。
没事还是应该多逛逛官方的文档的 有不少好东西。
也可以乱改改 试试有什么效果 - -
Color[] c = tex.GetPixels(0, 0, 5, 5); tex.SetPixels((int)pixelUV.x,(int)pixelUV.y,5,5,c);
源码上面链接就有了,由于不是特别多末尾在留一份吧,手懒直接粘:
// Write black pixels onto the GameObject that is located // by the script. The script is attached to the camera. // Determine where the collider hits and modify the texture at that point. // // Note that the MeshCollider on the GameObject must have Convex turned off. This allows // concave GameObjects to be included in collision in this example. // // Also to allow the texture to be updated by mouse button clicks it must have the Read/Write // Enabled option set to true in its Advanced import settings. using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public Camera cam; void Start() { cam = GetComponent<Camera>(); } void Update() { if (!Input.GetMouseButton(0)) return; RaycastHit hit; if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit)) return; Renderer rend = hit.transform.GetComponent<Renderer>(); MeshCollider meshCollider = hit.collider as MeshCollider; if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null) return; Texture2D tex = rend.material.mainTexture as Texture2D; Vector2 pixelUV = hit.textureCoord; pixelUV.x *= tex.width; pixelUV.y *= tex.height; tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, Color.black); tex.Apply(); } }
原文:http://www.cnblogs.com/guoguoguo/p/7241639.html
内容总结
以上是互联网集市为您收集整理的Unity一个弹痕的简单实现方法全部内容,希望文章能够帮你解决Unity一个弹痕的简单实现方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。