「Live2D Unity 文档翻译」定制材料

本文翻译自:https://docs.live2d.com/cubism-sdk-tutorials/unity-material-customization/

译者注:注意!这并不是一篇严谨的翻译,本人并不是翻译行业从业者,也根本不会日文。官网的中文翻译会连带代码一起翻译,而且还不如机翻日文,官网的英语翻译版本有的语法很奇怪,看起来也是机翻。本文主要来自日文机翻,然后再结合实际开发经验调整到通顺,修改不该翻译的东西并润色。

[最后更新日期: 2017/09/19] 译者注:这是这个日文原文的更新日期

by Traitam

总体概述

首先,在 Unity 上,我们将说明如何在 Unity 上使用 Cubism 模型。

之后,使用模型导入功能导入示例中的模型

在将 Cubism 模型导入 Unity 之前的准备

这是关于如何在 Unity 编辑器上显示 Cubism 模型的说明。

概述

UnityEditor 上,通常使用名为*Live2D/Cubism/Materials/Unlit*.mat*的材料进行显示

在导入模型时,会将材料应用于模型。

详细

在 Unity 上显示 Cubism 模型时,我们通常使用 Live2D/Cubism/Materials/Unlit*.mat 下的材料。

预期效果应该类似于 CubismEditor上的显示。

将材料应用于 Cubism 模型是在「将模型导入Unity Editor 」时完成的。

因此,将模型拖放到 Unity Editor 中时,将应用显示所需的材料。

Cubism 模型导入 Unity 时,将调用 CubismImporter.cs 中的 OnPickMaterial

在这里需要利用 CubismBuiltinPickers.MaterialPicker

在此方法中,将材料应用于 Cubism 模型中的每个 Drawable

之后,将对其进行预制,就可以在 Unity Editor 上自由处理。

如果要从脚本中自定义材料,则 OnPickMaterial 是切入点,因此您可以通过自己创建单独的方法并在 OnPickMaterial 调用来自定义材料。

接下来,作为示例,从脚本中自定义 Drawable 的材料,以便将其显示出来。

示例:自定义一个 Drawable 的材料

这是一个在导入模型时通过脚本更改指定 Drawable 的材料的示例。

概要

创建要指定的材料,并引用指定的Drawable的名称。

如果是被指定要自定义材料的 Drawable,则使用指定的材料,其余则不指定并使用普通材料。

编写进行上述处理的代码并将其分配给 OnPickMaterial 方法。

详细

在 Unity 编辑器上指定的 Drawable 名称指定可以在 CubismEditor 上确认的艺术网格的ID。

在 UnityEditor 中 Drawable 的名字是 CubismEditor 中艺术网格的 ID 。

img

img

UnityEditor 的 Drawable 名称与 CubismEditor 中的美术网格 ID 相关联,因此无法在 UnityEditor 中进行更改。

因此,如果要更改 Drawable 的名称,您需要在 Cubism Editor 上更改 ID 的名称。

这次,我们将更改整个头发的颜色,因此将使用头发的 Drawable。

将材料应用到头发的 Drawable 时,会应用自定义材料。

接下来,在 Unity Editor上创建 Material 文件夹并创建新材料。

我们将此材料称为 CustomMaterial。将此材料的着色器更改为 Sprites/Default

在这里,为了清晰起见,我们将更改 Tint 的颜色。在这里,它是红色的。

img

最后,创建一个 Editor 文件夹,并在该文件夹中创建一个新的 C#脚本。

在这里,名称写 MaterialCustomizer

在脚本中,引用指定的 Drawable,如果它是同名的 Drawable,则应用先前创建的材质。

对于其他 Drawable,则使用常规材质 (Live2D/Cubism/Materials/Unlit\.mat*)。

下面的代码包括该功能。

  1. using UnityEditor;
  2. using UnityEngine;
  3. using Live2D.Cubism.Core;
  4. using Live2D.Cubism.Framework.Json;
  5. using Live2D.Cubism.Editor.Importers;
  6. using System.Linq;
  7. namespace TraitamTutorials
  8. {
  9. /// <summary>
  10. /// Customizes materials of Cubism models from code.
  11. /// 通过代码自定义 Cubism 模型的材料。
  12. /// </summary>
  13. public static class MaterialCustomizer
  14. {
  15. /// <summary>
  16. /// IDs of drawables to customize.
  17. /// 要被自定义 drawables 的 IDs
  18. /// </summary>
  19. private static string[] _drawablesToCustomize = { "D_PSD_01" ,"D_PSD_02" ,"D_PSD_03" ,"D_PSD_04" ,"D_PSD_05" ,"D_PSD_68" ,"D_PSD_69" ,"D_PSD_70" ,"D_PSD_71"};
  20. /// <summary>
  21. /// Custom material to assign.
  22. /// 要被分配的自定义材料。
  23. /// </summary>
  24. private static Material _customMaterial;
  25. /// <summary>
  26. /// Make custom material available.
  27. /// 使自定义材料可用。
  28. /// And select <see cref="Material"/> as custom material or model default material.
  29. /// 然后选择 <see cref="Material"/> 作为自定义材质或模型默认材质。
  30. /// </summary>
  31. [InitializeOnLoadMethod]
  32. private static void RegisterMaterialInitialize()
  33. {
  34. _customMaterial = AssetDatabase.LoadAssetAtPath<Material>("Assets/TraitamTutorials/Materials/CustomMaterial.mat");
  35. CubismImporter.OnPickMaterial = CustomizeMaterial;
  36. }
  37. /// <summary>
  38. /// When <see cref="CubismDrawable.name"/> and <see cref="_drawablesToCustomize"/> the same, use custom material.
  39. /// 当 <see cref="CubismDrawable.name"/> 和 <see cref="_drawablesToCustomize"/> 相同时,使用自定义的材质
  40. /// Otherwise, use model default material.
  41. /// 其他情况下使用模型默认材质
  42. /// </summary>
  43. /// <param name="sender">Event source.</param>
  44. /// <param name="sender">事件发起者</param>
  45. /// <param name="drawable">Drawable to pick for.</param>
  46. /// <param name="drawable">要被自定义的 Drawable</param>
  47. /// <returns><see cref="Material"/> for using the model.</returns>
  48. /// <returns><see cref="Material"/> 要使用的模型</returns>
  49. private static Material CustomizeMaterial(CubismModel3Json sender, CubismDrawable drawable)
  50. {
  51. //use custom material.
  52. if (_drawablesToCustomize.Any(n => n == drawable.Id))
  53. {
  54. return _customMaterial;
  55. }
  56. //use default material.
  57. //使用默认材质
  58. return CubismBuiltinPickers.MaterialPicker(sender, drawable);
  59. }
  60. }
  61. }

现在,您可以在导入模型时为指定的 Drawable 自定义材质。

img