-
Notifications
You must be signed in to change notification settings - Fork 38
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
TheRealLyonic
wants to merge
3
commits into
SubnauticaModding:master
Choose a base branch
from
TheRealLyonic:ApplyGameMaterials
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
{ | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?