A component can be added to a model prefab using AssetPostprocessor.OnPostprocessModel. However, the component is only editable on instances of the prefab. Is there a way to make the component editable on the prefab itself?
How can components added to a model from an AssetPostprocessor be made editable on the model prefab?
Maybe this will help. In this example I am checking to make sure I only apply this to assets in a certain folder, then I create a loop that starts at the top level and drills down, in that loop I look for naming rules (how the thing was named). Finally you will see the lights rule where I delete the components of the imported object and add a new one (light) and set its properties.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using System;
public class PostProcessForMaya : AssetPostprocessor
{
private string flStructures = "/[ModEls]/StruCturEs/"; //Not case sensitive, we convert everything ToLower later
//strings for naming conventions. Mostly here so I can use .ToLower() on them
private string sLight = "Light";
private string sDeskPlaceHolder = "DeskPlaceHolder";
private void OnPostprocessModel(GameObject g)
{
string lowerCaseAssetPath = assetPath.ToLower();
if (lowerCaseAssetPath.IndexOf(flStructures.ToLower()) == -1)
return; //If it is not in the structures folder we wont post-process it
//Do something to each file found in the folder
Apply(g.transform);
//Lets make sure the devs know something just happened
Debug.Log(g.name + " has just been post-processed on " + DateTime.Now.ToString("dddd, dd-MMM-yyyy") + " at " + DateTime.Now.ToString("hh:mm tt"));
}
private void Apply(Transform transform)
{
//Make everything static for lightmapping
transform.gameObject.isStatic = true;
//Look for lights...
if (transform.name.ToLower().Contains(sLight.ToLower()))
LightRules(transform);
//Replace Desks
if (transform.name.ToLower().Contains(sDeskPlaceHolder.ToLower()))
DeskRules(transform);
foreach (Transform child in transform)
Apply(child); //cycle through all children of the current transform
}
private void LightRules(Transform transform)
{
//Remove colliders. Not needed for lights.
//Had to use UnityEngine.Object because we are also using "using.System" which has an Object reference
Component[] mr = transform.gameObject.GetComponentsInChildren(typeof(MeshRenderer), true);
foreach (MeshRenderer o in mr) { UnityEngine.Object.DestroyImmediate(o, true); }
Component[] mf = transform.gameObject.GetComponentsInChildren(typeof(MeshFilter), true);
foreach (MeshFilter o in mf) { UnityEngine.Object.DestroyImmediate(o, true); }
Component[] mc = transform.gameObject.GetComponentsInChildren(typeof(MeshCollider), true);
foreach (MeshCollider o in mc) { UnityEngine.Object.DestroyImmediate(o, true); }
//If it has a renderer then make it a light
transform.gameObject.AddComponent(typeof(Light));
//Set the light properties
transform.light.type = LightType.Point;
transform.light.shadows = LightShadows.Soft;
transform.light.shadowBias = 0.002f;
transform.light.intensity = 0.25f;
transform.light.range = 6.0f;
}
}