养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

【源码】Unity3D立方体使用代码自动UV映射

时间:2024-11-10 20:05:09

Unity3D立方体如何自动UV映射?简单几步,让你轻松解决。

【源码】Unity3D立方体使用代码自动UV映射

工具/原料

Unity3D

方法/步骤

1、1.打开软件,新建工程如下图所示

【源码】Unity3D立方体使用代码自动UV映射

2、2.首疏扌熘创建脚本文件如下图所示同时在【hierarchy】下面空白处鼠标右键创建空对象,并将脚本拖拽到该空对象下,如下图所示

【源码】Unity3D立方体使用代码自动UV映射

【源码】Unity3D立方体使用代码自动UV映射

3、3.双略居承柘击脚本用VS打开,如下图所示,输入如下代码usingSystem.Collections;usingSyst髫潋啜缅em.Collections.Generic;usingUnityEngine;publicclassCubeUVMaping:MonoBehaviour{//publicGameObjectpublicTexturegrassTexture;voidStart(){//Makesuretherequirementsareset.Otherwise,don'trunif(!grassTexture){Debug.Log("Failedtostart.Updatethetextures.");return;}//SetthetheGameObjectsoithastherequiredmeshmaterialsandsettingsgameObject.AddComponent<MeshFilter>();gameObject.AddComponent<MeshRenderer>();Meshmesh=GetComponent<MeshFilter>().mesh;mesh.Clear();//Createtheverticesforthecube.mesh.vertices=newVector3[]{//front-z(lookingatitfromtheside,thisisthecoord//thatletsyouseehowthinthetriangleis.Rememberthisfor//UVmapping.Itmakesthelogiceasier.)newVector3(0,0,0),newVector3(0,1,0),newVector3(1,1,0),newVector3(1,1,0),newVector3(1,0,0),newVector3(0,0,0),//back-znewVector3(0,0,1),newVector3(1,1,1),newVector3(0,1,1),newVector3(1,1,1),newVector3(0,0,1),newVector3(1,0,1),//right-xnewVector3(1,0,0),newVector3(1,1,0),newVector3(1,0,1),newVector3(1,1,1),newVector3(1,0,1),newVector3(1,1,0),//left-xnewVector3(0,0,0),newVector3(0,0,1),newVector3(0,1,0),newVector3(0,1,1),newVector3(0,1,0),newVector3(0,0,1),//top-ynewVector3(0,1,0),newVector3(0,1,1),newVector3(1,1,0),newVector3(1,1,0),newVector3(0,1,1),newVector3(1,1,1),//bottom-ynewVector3(0,0,0),newVector3(1,0,0),newVector3(0,0,1),newVector3(1,0,0),newVector3(1,0,1),newVector3(0,0,1)};Vector2[]uvs=newVector2[mesh.vertices.Length];for(inti=0;i<uvs.Length;){if(mesh.vertices[i].x==mesh.vertices[i+1].x&&mesh.vertices[i].x==mesh.vertices[i+2].x){uvs[i]=newVector2(mesh.vertices[i].y,mesh.vertices[i].z);uvs[i+1]=newVector2(mesh.vertices[i+1].y,mesh.vertices[i+1].z);uvs[i+2]=newVector2(mesh.vertices[i+2].y,mesh.vertices[i+2].z);}elseif(mesh.vertices[i].y==mesh.vertices[i+1].y&&mesh.vertices[i].y==mesh.vertices[i+2].y){uvs[i]=newVector2(mesh.vertices[i].x,mesh.vertices[i].z);uvs[i+1]=newVector2(mesh.vertices[i+1].x,mesh.vertices[i+1].z);uvs[i+2]=newVector2(mesh.vertices[i+2].x,mesh.vertices[i+2].z);}elseif(mesh.vertices[i].z==mesh.vertices[i+1].z&&mesh.vertices[i].z==mesh.vertices[i+2].z){uvs[i]=newVector2(mesh.vertices[i].x,mesh.vertices[i].y);uvs[i+1]=newVector2(mesh.vertices[i+1].x,mesh.vertices[i+1].y);uvs[i+2]=newVector2(mesh.vertices[i+2].x,mesh.vertices[i+2].y);}else{uvs[i]=newVector2(mesh.vertices[i].x,mesh.vertices[i].y);uvs[i+1]=newVector2(mesh.vertices[i+1].x,mesh.vertices[i+1].y);uvs[i+2]=newVector2(mesh.vertices[i+2].x,mesh.vertices[i+2].y);}i+=3;}mesh.uv=uvs;//Addthetrianglestorenderthecubeint[]triangleNumbers=newint[mesh.vertices.Length];for(inti=0;i<mesh.vertices.Length;i++)triangleNumbers[i]=i;mesh.triangles=triangleNumbers;//Setthecube'stextureandaddacolliderGetComponent<MeshRenderer>().material.mainTexture=grassTexture;gameObject.AddComponent<MeshCollider>();gameObject.transform.localScale=(newVector3(3,3,3));}}

【源码】Unity3D立方体使用代码自动UV映射

【源码】Unity3D立方体使用代码自动UV映射

【源码】Unity3D立方体使用代码自动UV映射

【源码】Unity3D立方体使用代码自动UV映射

【源码】Unity3D立方体使用代码自动UV映射

4、4.选择一张图片拖拽到【Project】面板下,并拖拽到【GameObject】的脚本中,如下图所示

【源码】Unity3D立方体使用代码自动UV映射

5、5.点击运行按钮,运行结果如下图所示。如何设置运行时界面的主题,请参见我的经验:

【源码】Unity3D立方体使用代码自动UV映射

6、6.立方体多个角度的效果,如下图所示

【源码】Unity3D立方体使用代码自动UV映射

【源码】Unity3D立方体使用代码自动UV映射

7、如果您觉得有用,记得在下方点击投票、点赞、关注、留言,小编会定期奉上更多的惊喜哦,您的支持才是小编继续努力的动力,么么哒。

【源码】Unity3D立方体使用代码自动UV映射

© 一点知识