当前位置:   article > 正文

Unity实战(4):Unity的纯参数化脚本生成或控制资产Assets_unity代码修改资产内容

unity代码修改资产内容

目录

前言

一、程序生成贴图Texture

二、程序生成材质Material

​三、程序挂载脚本Scripts

四、程序开关其他脚本(Scripts)或物体(GameObject)

五、程序生成网格


前言

我们在写unity脚本的时候,有时会碰到无法单纯使用手工拖拽或导入资产,需要程序脚本直接生成或控制一些资产的情况,因此本文的目的在于汇总这些操作方法。

一、程序生成贴图Texture

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ParamTexture : MonoBehaviour
  5. {
  6. public Texture2D proceduralTex;
  7. public Color texColor;
  8. private int width = 100;
  9. private int height = 100;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. texColor = new Color(1.0f, 0, 0, 0);
  14. proceduralTex = new Texture2D(width, height);
  15. for(int i = 0; i < width/2; i++) {
  16. for(int j = 0; j < height; j++) {
  17. proceduralTex.SetPixel(i, j, texColor);
  18. }
  19. }
  20. proceduralTex.Apply();
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. }
  26. }

说明:

需要新建一张贴图new Texture2D,然后使用for循环和SetPixel给每个像素上色。最后记得加上Apply应用。

把该脚本挂在主相机下,运行效果如下:

 

贴图如下: 

二、程序生成材质Material

生成材质就是在有贴图的情况下新建一个材质并把贴图作为主贴图(MainTexture)赋给材质。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ParamMaterial : MonoBehaviour
  5. {
  6. public Texture2D proceduralTex;
  7. public Material proceduralMat;
  8. public Color texColor;
  9. private int width = 100;
  10. private int height = 100;
  11. public GameObject ball0;
  12. // Start is called before the first frame update
  13. void Start() {
  14. texColor = new Color(1.0f, 0, 0, 0);
  15. proceduralTex = new Texture2D(width, height);
  16. proceduralMat = new Material(Shader.Find("Transparent/Diffuse"));
  17. for (int i = 0; i < width / 2; i++) {
  18. for (int j = 0; j < height / 2; j++) {
  19. proceduralTex.SetPixel(i, j, texColor);
  20. }
  21. }
  22. proceduralTex.Apply();
  23. proceduralMat.mainTexture = proceduralTex;
  24. ball0.GetComponent<MeshRenderer>().material = proceduralMat;
  25. }
  26. // Update is called once per frame
  27. void Update() {
  28. }
  29. }

说明:

在Unity中新建一个球Sphere, 将该脚本挂在球上,需要注意的是材质需要指定一个shader,这里使用的是Transparent/Diffuse,将贴图指定给材质的mainTexture,最后记得给物体附上材质,获取MeshRenderer下面的material,将材质赋给它。

效果如下:

 三、程序挂载脚本Scripts

首先准备一个需要挂载的脚本testScript.cs,效果是打开时会将物体尺寸放大5倍

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class testScript : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. this.transform.localScale = new Vector3(5, 5, 5);
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. }
  15. }

主代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ParamScripts : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. GameObject temp = GameObject.Find("Sphere");
  10. testScript script = temp.AddComponent<testScript>();
  11. }
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. }
  16. }

说明:

主代码挂载在主相机上,启动项目时根据名称获取物体(“Sphere”),然后添加Component

效果如下: 

 四、程序开关其他脚本(Scripts)或物体(GameObject)

开关脚本只需要在上一个案例中主代码添加一行

script.enabled = false;
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ParamScripts : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. GameObject temp = GameObject.Find("Sphere");
  10. testScript script = temp.AddComponent<testScript>();
  11. script.enabled = false;
  12. }
  13. // Update is called once per frame
  14. void Update()
  15. {
  16. }
  17. }

开关物体略有不同,需要在物体下添加一行

temp.SetActive(false);

五、程序生成网格

思路如下:

首先根据顶点vertices、UV、三角关系triangles新建一个Mesh;

然后新建一个物体GameObject挂载MeshFilter和MeshRenderer,将第一步的Mesh赋给MeshFilter的mesh

代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ParamMesh : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. //这里我们创建一个正方形网格,所以需要4个顶点、4个UV点和6条边
  10. Vector3[] vertices = new Vector3[4];
  11. Vector2[] uv = new Vector2[4];
  12. int[] triangles = new int[6];
  13. //声明顶点的位置
  14. vertices[0] = new Vector3(0, 1);
  15. vertices[1] = new Vector3(1, 1);
  16. vertices[2] = new Vector3(0, 0);
  17. vertices[3] = new Vector3(1, 0);
  18. //声明UV的位置
  19. uv[0] = new Vector2(0, 1);
  20. uv[1] = new Vector2(1, 1);
  21. uv[2] = new Vector2(0, 0);
  22. uv[3] = new Vector2(1, 0);
  23. //声明三角边,这里三角边是根据上面的顶点来进行连接的,每三个顶点构成一个三角边
  24. //这里后面的int类型参数对应 vertices[]数组中的顶点
  25. triangles[0] = 0;
  26. triangles[1] = 1;
  27. triangles[2] = 2;
  28. triangles[3] = 2;
  29. triangles[4] = 1;
  30. triangles[5] = 3;
  31. Mesh mesh = new Mesh();
  32. //将设置好的参数进行赋值
  33. mesh.vertices = vertices;
  34. mesh.uv = uv;
  35. mesh.triangles = triangles;
  36. GameObject gameObject = new GameObject("Mesh", typeof(MeshFilter), typeof(MeshRenderer));
  37. gameObject.transform.localScale = new Vector3(30, 30, 1);
  38. gameObject.GetComponent<MeshFilter>().mesh = mesh;
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. }
  44. }

将该脚本挂在主相机下,运行效果如下(未添加材质因此显示为洋红色): 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号