I’ve found that there are several custom functions and classes I would like to be able to instantiate/use from multiple scripts without including direct references to other scripts attached to game objects. For example, I’m practicing setting up a serialization manager, but as I want this to be able to serialize classes from multiple different kinds of scripts, I realized I would need a way to pass a class object to the save manager in a format it recognizes. I’m sure there’s a fancy way of doing this kind of stuff, but I’m just hoping there’s a way I can write a script that contains class definitions and functions that I can access from all scripts so I can instantiate the class object both in the client script and instantiate a copy in the save manager for serialization and then simply pass the object to the save manager.
I’m not really up on computer science lingo - I’m thinking what I need here is a custom library? How do implement and use one of those. For example, I instantiate an object of type “List” when using System.Collections.Generic. How do I create my own namespace functions/class definitions - can these only be written in C++ or other another lower level language than C#?
And then pop using namespacename; at the top of of the script that should refer to it.
Namespace is only used to organize your code into smaller chunk.
For example, UnityEngine.Object is not the same as System.Object, and yet they are both name “Object”.
Creating a namespace is as easy as writing it;
namespace MySpace
{
public class MyClass { }
}
Just doing that, and MyClass is part of MySpace and can be called MySpace.MyClass.
As for making a class able to react to multiple type, it’s a completely different issue. You might want to look at Generic Method; Generic Methods - C# | Microsoft Learn
Example of generic serialization;
public static void Serialize<T>(T instance)
{
foreach (FieldInfo info in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
{
object value = info.GetValue(instance);
// Write Value somewhere
}
}
public static T Deserialize<T>(Stream input)
{
T output = default(T);
foreach (FieldInfo info in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
{
//Get value from stream
info.SetValue(output, value);
}
return output;
}
Do I have to compile an external DLL to create my own namespace or is there a place in the Unity Project I can insert this script and it will find it when another script attempts to use the namespace? Or does it automatically know the contents of all scripts in the project?
You never have to compile a DLL for Unity. Simply encapsulating your script in a namespace is enough to create and assign said class to that namespace.
Let’s say I do
namespace MySpace
{
public class MyClass { }
}
You won’t get access to MyClass from somewhere else in your code unless;
- You declare using that namepace;
using MySpace;
- Your class is within that same namespace;
namespace MySpace
{
public class MyOtherClass { }
}
- You explicitly reference that namescape;
MySpace.MyClass instance = new MySpace.MyClass();
But first, read the C# documentation about namespace; Organizing types in namespaces - C# | Microsoft Learn
Yes, I’m just trying to determine that, if I do not compile an external DLL, where in the Unity project do I stick the “MyNamespaceDefinition.cs” file for “MyScript.cs” to be able to implement “using MyNamespace;”? Or does it matter due to the fact that Unity compiles all the scripts? Does the “MyNamespaceDefinition.cs” have to be attached to a game object to be available in a scene, for example? I’m just not to clear on how Unity evaluates the individual assets folders in the project.
What is “MyNamespaceDefinition.cs”? What’s in it?
Nevermind, it was just a “pseudocode” name for the script file that contained the namespace definition (i.e. namespace ThisNamespace { … }). The file doesn’t actually exist - I know people usually post real code that they’re trying to impliment here on the forums, I just post code I make up on the spot to try to grasp the syntax/method I must employ to get at my objective.
However, I just tried implementing a namespace by just leaving the namespace definition in a file in the assets of the project and a gameobject script seems to be able to access the namespace. So, thanks to you, I have learned that I didn’t in fact have to be working extra carefully not to make mistakes in Visual Studio to make a DLL I would add to the project. Thanks for that.