Unity实战(4):Unity的纯参数化脚本生成或控制资产Assets

目录

前言

一、程序生成贴图Texture

二、程序生成材质Material

​三、程序挂载脚本Scripts

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

五、程序生成网格


前言

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

一、程序生成贴图Texture

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParamTexture : MonoBehaviour
{
    public Texture2D proceduralTex;
    public Color texColor;
    private int width = 100;
    private int height = 100;
    // Start is called before the first frame update
    void Start()
    {
        texColor = new Color(1.0f, 0, 0, 0);
        proceduralTex = new Texture2D(width, height);
        for(int i = 0; i < width/2; i++) {
            for(int j = 0; j < height; j++) {
                proceduralTex.SetPixel(i, j, texColor);
            }
        }
        proceduralTex.Apply();
    }

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

说明:

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

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

 

贴图如下: 

二、程序生成材质Material

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParamMaterial : MonoBehaviour
{
    public Texture2D proceduralTex;
    public Material proceduralMat;
    public Color texColor;
    private int width = 100;
    private int height = 100;
    public GameObject ball0;
    // Start is called before the first frame update
    void Start() {
        texColor = new Color(1.0f, 0, 0, 0);
        proceduralTex = new Texture2D(width, height);
        proceduralMat = new Material(Shader.Find("Transparent/Diffuse"));
        for (int i = 0; i < width / 2; i++) {
            for (int j = 0; j < height / 2; j++) {
                proceduralTex.SetPixel(i, j, texColor);
            }
        }
        proceduralTex.Apply();

        proceduralMat.mainTexture = proceduralTex;
        ball0.GetComponent<MeshRenderer>().material = proceduralMat;
    }

    // Update is called once per frame
    void Update() {

    }
}

说明:

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

效果如下:

 三、程序挂载脚本Scripts

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testScript : MonoBehaviour
{
    
    // Start is called before the first frame update
    void Start()
    {
        this.transform.localScale = new Vector3(5, 5, 5);
    }

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

主代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParamScripts : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject temp = GameObject.Find("Sphere");
        testScript script = temp.AddComponent<testScript>();
    }

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

说明:

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

效果如下: 

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

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

script.enabled = false;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParamScripts : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject temp = GameObject.Find("Sphere");
        testScript script = temp.AddComponent<testScript>();
        script.enabled = false;
    }

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

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

temp.SetActive(false);

五、程序生成网格

思路如下:

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

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

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParamMesh : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //这里我们创建一个正方形网格,所以需要4个顶点、4个UV点和6条边
        Vector3[] vertices = new Vector3[4];
        Vector2[] uv = new Vector2[4];
        int[] triangles = new int[6];
        //声明顶点的位置
        vertices[0] = new Vector3(0, 1);
        vertices[1] = new Vector3(1, 1);
        vertices[2] = new Vector3(0, 0);
        vertices[3] = new Vector3(1, 0);
        //声明UV的位置
        uv[0] = new Vector2(0, 1);
        uv[1] = new Vector2(1, 1);
        uv[2] = new Vector2(0, 0);
        uv[3] = new Vector2(1, 0);
        //声明三角边,这里三角边是根据上面的顶点来进行连接的,每三个顶点构成一个三角边
        //这里后面的int类型参数对应 vertices[]数组中的顶点
        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 2;
        triangles[3] = 2;
        triangles[4] = 1;
        triangles[5] = 3;
        Mesh mesh = new Mesh();
        //将设置好的参数进行赋值
        mesh.vertices = vertices;
        mesh.uv = uv;
        mesh.triangles = triangles;
        GameObject gameObject = new GameObject("Mesh", typeof(MeshFilter), typeof(MeshRenderer));
        gameObject.transform.localScale = new Vector3(30, 30, 1);
        gameObject.GetComponent<MeshFilter>().mesh = mesh;
    }

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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity提供了一些命令行工具来支持自动化构建,例如Unity编辑器的批处理模式和Unity命令行工具。Jenkins是一种流行的自动化构建工具,可以通过编写脚本来自动化Unity项目的构建和部署。以下是一个简单的Jenkins脚本示例: ``` stage('Build Unity Project') { steps { sh "unity -batchmode -quit -projectPath /path/to/project -executeMethod BuildScript.PerformBuild -logFile /path/to/logfile.log" } } ``` 在这个脚本中,我们使用Unity批处理模式来构建Unity项目。`-batchmode`参数告诉Unity以批处理模式运行,`-quit`参数告诉Unity在完成操作后关闭编辑器,`-projectPath`参数指定项目路径,`-executeMethod`参数指定要执行的方法,`-logFile`参数指定日志文件路径。 在你的Unity项目中,你需要编写一个自定义的编辑器脚本来实现构建逻辑。这个脚本需要继承自`EditorWindow`或`Editor`类,并实现一个名为`PerformBuild`的静态方法。在这个方法中,你可以使用Unity的API来自定义构建流程,例如导出游戏包或生成可执行文件。 ``` public static void PerformBuild() { BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); buildPlayerOptions.scenes = new[] { "Assets/Scenes/Main.unity" }; buildPlayerOptions.locationPathName = "/path/to/build"; buildPlayerOptions.target = BuildTarget.StandaloneWindows64; buildPlayerOptions.options = BuildOptions.None; BuildPipeline.BuildPlayer(buildPlayerOptions); } ``` 在这个示例中,我们使用BuildPipeline类来构建游戏包。我们指定了要打包的场景、输出路径、目标平台和构建选项。 在Jenkins中,你可以将以上脚本复制到一个新的构建步骤中,并配置好Unity的安装路径、项目路径和日志文件路径。当你运行Jenkins构建时,它将自动构建你的Unity项目,并将日志输出到指定的文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值