-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from KuraiAndras/develop
9.0.2
- Loading branch information
Showing
7 changed files
with
143 additions
and
77 deletions.
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# 9.0.2 | ||
- Fix override handling when running the editor tool | ||
|
||
# 9.0.1 | ||
- Fix Serilog Template | ||
|
||
|
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
2 changes: 1 addition & 1 deletion
2
UnityProject/Injecter.Unity/Assets/Injecter.Hosting.Unity/package.json
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
105 changes: 105 additions & 0 deletions
105
UnityProject/Injecter.Unity/Assets/Injecter.Unity/Editor/GameObjectPatcher.cs
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using UnityEditor; | ||
using UnityEditor.SceneManagement; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Injecter.Unity.Editor | ||
{ | ||
internal delegate bool FilterGameObject(GameObject gameObject); | ||
internal delegate void PatchGameObject(GameObject gameObject, string location); | ||
|
||
internal static class GameObjectPatcher | ||
{ | ||
public static void AddComponentsToEveryPrefab(FilterGameObject filter, PatchGameObject patch) | ||
{ | ||
foreach (var prefabPath in AssetDatabase.GetAllAssetPaths().Where(s => s.EndsWith(".prefab"))) | ||
{ | ||
var prefab = AssetDatabase.LoadMainAssetAtPath(prefabPath); | ||
|
||
if (prefab is GameObject prefabObject) | ||
{ | ||
AddScriptsToGameObject(prefabObject, filter, patch, $"prefab: {prefabPath}"); | ||
AssetDatabase.SaveAssets(); | ||
} | ||
} | ||
} | ||
|
||
public static void AddComponentsToCurrentScene(FilterGameObject filter, PatchGameObject patch) | ||
{ | ||
var sceneName = SceneManager.GetActiveScene().name; | ||
|
||
var rootObjects = SceneManager | ||
.GetActiveScene() | ||
.GetRootGameObjects(); | ||
|
||
foreach (var gameObject in rootObjects) | ||
{ | ||
AddScriptsToGameObject(gameObject, filter, patch, $"scene: {sceneName}"); | ||
} | ||
} | ||
|
||
public static void AddComponentsToEveryScene(FilterGameObject filter, PatchGameObject patch) | ||
{ | ||
var dataPathFull = Path.GetFullPath(Application.dataPath); | ||
var scenes = Directory | ||
.EnumerateFiles(dataPathFull, "*.unity", SearchOption.AllDirectories) | ||
.Select(s => s.Replace(dataPathFull, string.Empty)) | ||
.Select(s => $"Assets{s}") | ||
.ToArray(); | ||
|
||
var originalScenePath = SceneManager.GetActiveScene().path; | ||
EditorSceneManager.SaveOpenScenes(); | ||
|
||
foreach (var scene in scenes) | ||
{ | ||
EditorSceneManager.OpenScene(scene); | ||
AddComponentsToCurrentScene(filter, patch); | ||
EditorSceneManager.SaveOpenScenes(); | ||
} | ||
|
||
EditorSceneManager.OpenScene(originalScenePath); | ||
} | ||
|
||
public static void AddComponentsToEverything(FilterGameObject filter, PatchGameObject patch) | ||
{ | ||
AddComponentsToEveryPrefab(filter, patch); | ||
AddComponentsToEveryScene(filter, patch); | ||
} | ||
|
||
public static void EnsureComponentSafe<T>(MonoBehaviour instance, GameObject holder, string location) where T : Component | ||
{ | ||
if (instance.TryGetComponent<T>(out _)) return; | ||
|
||
var component = Undo.AddComponent<T>(holder); | ||
|
||
if (component == null) return; | ||
|
||
if (PrefabUtility.IsAddedComponentOverride(component) && !PrefabUtility.IsAddedGameObjectOverride(holder)) | ||
{ | ||
Undo.DestroyObjectImmediate(component); | ||
} | ||
else | ||
{ | ||
Debug.Log($"Adding script: {typeof(T).Name} to GameObjec: {holder.name} at {location}", holder); | ||
} | ||
} | ||
|
||
private static void AddScriptsToGameObject(GameObject gameObject, FilterGameObject filter, PatchGameObject patch, string location) | ||
{ | ||
var instances = gameObject | ||
.GetComponentsInChildren<Transform>(true) | ||
.Where(t => t != null && filter(t.gameObject)) | ||
.Select(t => t.gameObject) | ||
.ToArray(); | ||
|
||
for (var i = 0; i < instances.Length; i++) | ||
{ | ||
var instance = instances[i]; | ||
if (instance == null) continue; | ||
patch(instance, location); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
UnityProject/Injecter.Unity/Assets/Injecter.Unity/Editor/GameObjectPatcher.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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