Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

61 行
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.Editor
{
public class NullMaterialCheck : Check
{
public override string name => "Renderer/Null Material";
public override bool defaultEnabled => true;
public override string[] ResolutionActions => new string[] { "Assign Default Material" };
public override int defaultResolutionActionIndex => 0;
public override IEnumerable<CheckResult> GetResults(SceneObjects sceneObjects)
{
foreach(var obj in sceneObjects.sceneObjects)
{
if(obj.TryGetComponent(out Renderer r))
{
foreach(var mat in r.sharedMaterials)
{
if(mat == null)
{
yield return new CheckResult(this, CheckResult.Result.Failed, $"Missing Material for Object {obj.name}", obj);
break;
}
}
}
}
}
static Material s_DefaultMaterial;
public override void Resolve(CheckResult result)
{
if(s_DefaultMaterial == null)
{
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
s_DefaultMaterial = cube.GetComponent<Renderer>().sharedMaterial;
Object.DestroyImmediate(cube);
}
switch (result.resolutionActionIndex)
{
default:
var renderer = (result.mainObject as GameObject).GetComponent<Renderer>();
for(int i = 0; i<renderer.sharedMaterials.Length; i++)
{
if(renderer.sharedMaterials[i] == null)
{
renderer.materials[i] = s_DefaultMaterial;
}
}
break;
}
}
}
}