Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of ApplyGameMaterials method #583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion Nautilus/Utility/MaterialUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public enum MaterialType
/// </summary>
Transparent,
/// <summary>
/// Transparent pixels on the texture are not renderered. Useful for decals.
/// Transparent pixels on the texture are not rendered. Useful for decals.
/// </summary>
Cutout
}
Expand Down Expand Up @@ -272,6 +272,65 @@ public static void ApplyUBERShader(Material material, float shininess, float spe
}
}

/// <summary>
/// Applies base-game materials to custom <see cref="GameObject"/>s by matching the name of every material in every
/// renderer on your custom object (and any of its children) against those on the base-game object's renderers,
/// and any of its children. To use this class, go in-game, and lookup the name of the material you wish to apply
/// to your custom object using runtime editor. In Unity, rename the materials on your object(s) to match the name
/// of the material(s) in-game. Then, pass both your custom object, and the base-game object that the material(s)
/// you want to apply are on, to this method, and the game's material(s) will replace the ones you made. Passing
/// in parent-objects to those with the materials on them will also work.
/// </summary>
/// <param name="customPrefab">The custom <see cref="GameObject"/> you want to apply base-game materials to.</param>
/// <param name="baseGamePrefab">The base-game prefab which contains the material(s) you want to apply.</param>
public static void ApplyGameMaterials(GameObject customPrefab, GameObject baseGamePrefab)
{
if (customPrefab is null || baseGamePrefab is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't necessarily trust the is keyword when it comes to Unity. I would change it. Can someone confirm?

Suggested change
if (customPrefab is null || baseGamePrefab is null)
if (customPrefab == null || baseGamePrefab == null)

{
InternalLogger.Error("Couldn't apply game materials because a null object was passed into the method.");
return;
}

var customRenderers = customPrefab.GetComponentsInChildren<Renderer>(true);

for (int i = 0; i < customRenderers.Length; i++)
{
var newMaterialList = customRenderers[i].materials;

for (int j = 0; j < newMaterialList.Length; j++)
{
bool matFound = false;

foreach (var baseGameRenderer in baseGamePrefab.GetComponentsInChildren<Renderer>(true))
{
foreach (var baseGameMat in baseGameRenderer.materials)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should null materials be accounted for? I don't think this method should throw exceptions for them

{
string baseMatName = baseGameMat.name.ToLower();
string plainBaseMatName = "";

//Removes the (instance) portion of the mat name, if it's present.
if (baseMatName.Contains("(instance)"))
plainBaseMatName = baseMatName.Substring(0, baseMatName.LastIndexOf('(') - 1);

string customMatName = newMaterialList[j].name.ToLower();
if (customMatName.Equals(baseMatName) || customMatName.Equals(plainBaseMatName))
{
newMaterialList[j] = baseGameMat;

matFound = true;
break;
}
}

if (matFound)
break;
}
}

customRenderers[i].materials = newMaterialList;
}
}

/// <summary>
/// Toggle the transparency on a material.
/// </summary>
Expand Down
Loading