Skip to content
Snippets Groups Projects
RenderSettingApplier.cs 1.48 KiB
Newer Older
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RenderSettingApplier {

	public void Apply(IRenderSettingPreset preset) {
		foreach (VisualBlueprint b in preset.GetVisualBlueprints()) {
			Apply(b);
		}
	}

	public void Apply(VisualBlueprint blueprint) {

		try {
			GameObject[] allObjects = GameObject.FindGameObjectsWithTag(blueprint.GetName());
			foreach (GameObject currObject in allObjects) {
				if (currObject.activeInHierarchy) {
					ReplaceVisualComponents(currObject, blueprint.GetBlueprintObject());
				}
				Debug.Log("Applying blueprint: " + blueprint.GetName() + " to object: " + currObject.name);
			}
		} catch (UnityEngine.UnityException e) {
			Debug.Log("No objects in scene with tag: " + blueprint.GetName() + " exist");
			Debug.Log(e);
	}

	/**
		Replace the visual components of the target with those of the reference
	**/
	private void ReplaceVisualComponents(GameObject target, GameObject reference)
	{
		HashSet<System.Type> componentTypes = new HashSet<System.Type>();
		componentTypes.Add(typeof(Collider));
		componentTypes.Add(typeof(MeshFilter));
		componentTypes.Add(typeof(Renderer));
		foreach (System.Type t in componentTypes) {
			Component targetComponent = target.GetComponent(t);
			Undo.RecordObject(targetComponent, "Paste setting blueprint values");
			EditorUtility.CopySerializedIfDifferent(reference.GetComponent(t), targetComponent);
			EditorUtility.SetDirty(targetComponent);