OkashiKami
OkashiKami
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
i would of never guess thank you so much for your help very much appreciate it dam much love
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
your right ugh dam are you serious rn
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
ah probably
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
yeah it seams like it's creating and instance of the base class not the actual class of the type but what it inherits from
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
i even show elements as we are stepping
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
here me stepping through the code
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
let me try with just type
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
so you know how on msdn they have CreateInstance(String, String) Creates an instance of the type whose name is specified, using the named assembly and parameterless constructor. for some reason that one is not available to me
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
do you thing it's because those classes inherits a base class in my case Modifier.cs
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
yes true one thing i did notice is that the exception doesn't get fired unless i put it in a try catch otherwise it doesn't do anything
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
and i expect when i rand the Activator.CI to return an object of the type that was provided but instead i get a null value when after inspecting the elements with breakpoints all is good but not that part
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
while stepping as soon as i get to the section that says Activator.createInstance it return null like i said not really sure i'm getting a MissingMethodException: Constructor on type 'OkashiKami.Terragen.Modifiers.SmoothModifier' not found. exception but all my classes have a default constructor in them so i really don't understand
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
i event stepped through the code and everything is correct up to the section of the fist codebase right after var classes = new List<object>(); everything after that fails
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
The method that calls the function
var classes = Utilities.GetClasses(typeof(Modifier), "OkashiKami.Terragen.Modifiers").ToList();
foreach (var _class in classes)
{
var name = _class.ToString().Split('.').Last();
if (name.Equals(nameof(ChunckModifier)))
{
if (self.me.modifiers.Find(x => x.GetType().Equals(typeof(ChunckModifier))) != null)
{
menu.AddDisabledItem(new GUIContent(name));
continue;
}
}

menu.AddItem(new GUIContent(name), false, ProcessDropDownButtonClick, _class);
}
var classes = Utilities.GetClasses(typeof(Modifier), "OkashiKami.Terragen.Modifiers").ToList();
foreach (var _class in classes)
{
var name = _class.ToString().Split('.').Last();
if (name.Equals(nameof(ChunckModifier)))
{
if (self.me.modifiers.Find(x => x.GetType().Equals(typeof(ChunckModifier))) != null)
{
menu.AddDisabledItem(new GUIContent(name));
continue;
}
}

menu.AddItem(new GUIContent(name), false, ProcessDropDownButtonClick, _class);
}
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
Sub Classes
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
original Class
61 replies
CC#
Created by OkashiKami on 11/26/2022 in #help
❔ Unity3D C-Sharp Create class instance from Type
The function that does the conversion
public static IEnumerable<object> GetClasses(Type type, string _namespace = null, Predicate<Type> predicate = null)
{
var assembly = type.Assembly;
var results = new List<Type>();
IEnumerable<Type> types = assembly.GetTypes();
if (!string.IsNullOrEmpty(_namespace))
results.AddRange(types.Where(x => string.Equals(type.Namespace, _namespace, StringComparison.Ordinal)));
results = results.Where(x => Equals(x.BaseType, type)).ToList();
if (predicate != null)
results = results.Where(t => predicate(t)).ToList();

var classes = new List<object>();
foreach (var result in results)
{
try
{
var name = result.FullName;
var _class = Activator.CreateInstance(result, BindingFlags.Public);
if (_class != null)
classes.Add(_class);
}
catch(Exception ex)
{
Debug.LogException(ex);
}
}

return classes;
}
public static IEnumerable<object> GetClasses(Type type, string _namespace = null, Predicate<Type> predicate = null)
{
var assembly = type.Assembly;
var results = new List<Type>();
IEnumerable<Type> types = assembly.GetTypes();
if (!string.IsNullOrEmpty(_namespace))
results.AddRange(types.Where(x => string.Equals(type.Namespace, _namespace, StringComparison.Ordinal)));
results = results.Where(x => Equals(x.BaseType, type)).ToList();
if (predicate != null)
results = results.Where(t => predicate(t)).ToList();

var classes = new List<object>();
foreach (var result in results)
{
try
{
var name = result.FullName;
var _class = Activator.CreateInstance(result, BindingFlags.Public);
if (_class != null)
classes.Add(_class);
}
catch(Exception ex)
{
Debug.LogException(ex);
}
}

return classes;
}
61 replies