包含此页的版本:
不含此页的版本:
您可以使用 C#脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表与 Unity 的内置导入器交互,或创建脚本导入器以添加对 Unity 本机不支持的文件的支持。
使用AssetPostprocessorclass 在 Unity 启动其内置导入程序的导入过程之前或之后添加自定义行为。您可以在导入过程中作导入设置、分析导入的资产或动态生成新资产。请参阅支持的资产类型参考,了解可用内置导入器的完整列表。
下面是一个示例AssetPostprocessor在导入纹理之前修改纹理的导入设置,然后在导入后将红色色调应用于纹理的脚本:
using UnityEngine;
using UnityEditor;
public class CustomTextureImporter : AssetPostprocessor
{
// Increment the version number, when the AssetPostprocessors code/behavior is changed
static readonly uint k_Version = 0;
public override uint GetVersion() { return k_Version; }
void OnPreprocessTexture()
{
// Get a reference to the TextureImporter
TextureImporter importer = assetImporter as TextureImporter;
// Customize settings
importer.mipmapEnabled = false;
importer.textureType = TextureImporterType.Default;
importer.maxTextureSize = 512;
importer.wrapMode = TextureWrapMode.Repeat;
Debug.Log($"Texture '{assetPath}' has had its import settings changed in OnPreProcessTexture.");
}
void OnPostprocessTexture(Texture2D texture)
{
// Set a red color tint to the texture
Color tintColor = new(1.0f, 0.5f, 0.5f, 1.0f);
// Get the texture's pixels
Color[] pixels = texture.GetPixels();
for (int i = 0; i < pixels.Length; i++)
{
// Apply the tint color
pixels[i] *= tintColor;
}
// Set the modified pixels back to the texture
texture.SetPixels(pixels);
// Apply the changes to the texture
texture.Apply();
// Log the change
Debug.Log($"Texture '{texture.name}' has been tinted with a red color in OnPostProcessTexture.");
}
}
要使用此示例,请将其放置在项目的Assets文件夹,然后将新纹理添加到Assets文件夹。然后,Unity 将设置应用于纹理,如下图所示:
若要添加对 Unity 本身不支持的文件格式的支持,可以使用ScriptedImporter以在 C# 中编写自定义资产导入器。
脚本导入器是继承自抽象类的类ScriptedImporter并且具有[ScriptedImporter]属性。这将注册您的自定义导入器以处理一个或多个文件扩展名。当 Unity 检测到与已注册文件扩展名匹配的文件是新的或已更改的文件时,它会调用该方法OnImportAsset您的定制进口商。
重要:脚本导入器无法处理 Unity 已经本机处理的文件扩展名。您可以使用overrideExts参数来覆盖此行为并为现有导入器添加文件扩展名。有关 Unity 原生支持的文件列表,请参阅支持的资产类型参考。
添加ScriptedImporter脚本添加到项目中,您可以像使用 Unity 支持的任何其他文件类型一样使用它。有关更多信息,请参阅导入资产简介。
以下代码示例导入扩展名为cube变成一个预制件:一种资产类型,允许您存储包含组件和属性的游戏对象。预制件充当模板,你可以从中在场景中创建新的对象实例。更多信息
请参阅术语表将立方体图元作为主要资源,并默认材质和颜色。然后,它从从资产文件读取的值分配其位置:
using UnityEngine;
using UnityEditor.AssetImporters;
using System.IO;
// The importer is registered with Unity's asset pipeline by placing the ScriptedImporter attribute on the
// CubeImporter class. The CubeImporter class implements the abstract ScriptedImporter base class.
[ScriptedImporter(1, "cube")]
public class CubeImporter : ScriptedImporter
{
public float m_Scale = 1;
// The ctx argument contains both input and output data for the import event
public override void OnImportAsset(AssetImportContext ctx)
{
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
var position = JsonUtility.FromJson<Vector3>(File.ReadAllText(ctx.assetPath));
cube.transform.position = position;
cube.transform.localScale = new Vector3(m_Scale, m_Scale, m_Scale);
// 'cube' is a GameObject and is automatically converted into a prefab.
// Only the 'Main Asset' is eligible to become a prefab.
ctx.AddObjectToAsset("main obj", cube);
ctx.SetMainObject(cube);
var material = new Material(Shader.Find("Standard"));
material.color = Color.red;
// Assets must be assigned a unique identifier string consistent across imports.
ctx.AddObjectToAsset("my Material", material);
// Assets that are not passed into the context as import outputs must be destroyed.
var tempMesh = new Mesh();
DestroyImmediate(tempMesh);
}
}
有关更多信息,请参阅AssetImporters.ScriptedImporterAPI 文档.
要为脚本导入器创建自定义导入设置窗口,请创建一个继承自ScriptedImporterEditor并用[CustomEditor]属性。例如:
using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEditor.SceneManagement;
using UnityEngine;
[CustomEditor(typeof(CubeImporter))]
public class CubeImporterEditor: ScriptedImporterEditor
{
public override void OnInspectorGUI()
{
var colorShift = new GUIContent("Color Shift");
var prop = serializedObject.FindProperty("m_ColorShift");
EditorGUILayout.PropertyField(prop, colorShift);
base.ApplyRevertGUI();
}
}
Unity 对以下文件格式使用脚本导入器:
.abc文件类型。.usd文件类型。