string first = “Assets/X-UniTMX/Code/Map.cs (2684,5)”;
string second = obj.GetPropertyAsString(Property_AddComponent + c);
UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, first, second);
I’m using a tiled loader (X_UniTMX) and it has some obsolete code which is preventing me to build the project.
I get this error :
Assets/X-UniTMX/Code/Map.cs(2685,63): error CS0619: `UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(UnityEngine.GameObject, string, string)' is obsolete: `Method is not meant to be used at runtime. Please, replace this call with GameObject.AddComponent<T>()/GameObject.AddComponent(Type).'
I had the same issue. You have to get the type by System.Type.GetType(type) and use gameObject.AddComponent( thatType).
Here is an example of how I fixed it.
public MegaModifier Add(string type)
{
/****************** The issue code **************************************
MegaModifier mod = (MegaModifier)UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/Mega-Fiers/Scripts/MegaFiers/Modifiers/MegaModifiers.cs (614,36)", type);
if ( mod != null )
BuildList();
return mod;
*******************************************************/
//Bug Fixed for building on device
//This API is no longer being used.
var theType = System.Type.GetType(type);
if (theType == null || !(typeof(Component).IsAssignableFrom(theType)))
return null;
MegaModifier mod = (MegaModifier)gameObject.AddComponent(theType);
if (mod != null)
BuildList();
return mod;
}
Well, that framework used an internal API that was never meant to be used by any usercode. This method is / was part of Unity’s automatic script updating service. This AddComponent method ist just a wrapper around the usual gameObject.AddComponent() but with a systematic search for the class type that is provided as string. It first checked if the type name specifies a type in the UnityEngine.dll. If no type was found it continued in the current assembly (which would include most of your own custom scripts) and if still no type was found it searches through all loaded assemblies.
Basically something like that:
using System.Linq;
// [ .. ]
static System.Type ResolveType(string aName)
{
System.Type result = typeof(Component).Assembly.GetTypes().Where(typeFromHandle.IsAssignableFrom).FirstOrDefault(t => (t.Name == aName|| t.FullName == aName);
if (result != null)
return result;
result = System.Assembly.GetCallingAssembly().GetType(aName);
if (result != null)
return result
result = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(a=>a.GetTypes()).SingleOrDefault(t=> t.Name == aName && typeof(Component).IsAssignableFrom(t));
return result;
}
Now you can replace those 3 lines with
string componentName = obj.GetPropertyAsString(Property_AddComponent + c);
var type = ResolveType(componentName);
if (type != null)
gameObject.AddComponent(type);
Note that all code here hasn’t been syntax checked since I’ve directly written it here on UA.