- Home /
Custom _GetComponent( )
Hello dear community!
I am looking for a way to avoid the use of GetComponent ( ) completely, my language is c#.
Could you please point me in the direction where I should search at, all I need is to reference a script (maybe somehow from an allocated memory), located in a game object.
That is, reference script instance on a game object (maybe through a pointer), perhaps with my own _GetComponent( ) function, to dodge Unity's API.
Even if you tell me how to open unity's shipped code with GameObject class code, will mean a lot to my search.
Thank you very much!
EDIT: I've added the code, sadly the previous answers aren't the ones I am looking for The problem is shown in the last 3 lines using UnityEngine; using UnityEditor; using System.Threading
[CustomEditor(typeof(ScriptA))]
public class ScriptAEditor{
//vars declarations
void OnEnable(){
GameObject somevarOfTypeGameObj = (GameObject)target; //This game object will have ScriptZ on it
}
void OnInspectorGUI{
object a1 = somevarOfTypeGameObj;
Thread t1 = new Thread(new ParameterizedThreadStart(functionX));
t1.Start(a1);
}//end onInspectorGUI
void functionX(object _work){
ScriptZ lookat = (ScriptZ)_work; //will not work, as unity says somevarOfTypeGameObject cannot be casted as ScriptZ.
//Default approach is to use standard GetComponent, which won't wok with threads
//threading is highly required as functionX is recursive
//getting component ScriptZ and passing it into the function before the thread starts won't work, since functionX is recursive and
//accesses different instances of scripts from different game objects
}
}
Is somevarOfTypeGameObj a component or just a variable?
Your example is full of holes.
It's a variable of type GameObject. It's value is assigned inside OnEnable()
Look HERE.
You can't access components off the main thread. You have to package all of your data up, send it to the thread and then wait for them to return and then apply it.
If you are building component/gameobject structures in your threads, you will have to model the structure on the thread and then actually build it in the main thread.
It may be a better solution to create lines of communication between your threads and the main thread. Then, if a thread needs to lookup a component on demand, it can request it from the main thread. But, the main thread would still need to package the component data in a new form before sending it back.
The link I posted describes a Loom which is very powerful. Check it out.
Spineer, could you give me a hand on this one as well? here
Answer by Spinnernicholas · Dec 30, 2013 at 06:04 PM
Unity doesn't expose the underlying component structure. I'm guessing that's what you really want to do.
If you want to know this in order to extend the functionality, then you can define your own and fall through to the original if needed:
T _GetComponent<T>()
{
//Do your code
//else
return GetComponent<T>()
}
Some of the built in components are exposed in the MonoBehavior type. IE: Animation, Transform, etc....
But, for your custom components, you can have the components send messages and then have the other components receive them.
gameObject.SendMessage("MyComponentStarting");
and then:
void MyComponentStarting()
{
//store reference
}
You would want to make this more generic, but that's the basic idea.
These are really your only options.
thank you, but sadly, that's not what I am looking for :(
I need to reference a script, that's probably stored in some form of list on that game object's object, there has to be a away!
I am working with threads, so there is a need to avoid API
I've updated the script to show what I mean
I started by saying exactly what you wanted. Unity has the component list hidden. There is no way to get at it without hacking into Unity's Binaries.
Oh, thank you. Was that Unity Threads link helpful?
Yep) I knew about it before but now I think about baking those components into a list;
When the game will start - I never need to GetComponent(), since I can access the scripts from list by index
Yeah, that will work. And it should be fairly easy to program. But, you might have problems manipulating the components inside of the threads. I haven't tried anything like that, but if you run into any snags, let me know.
Answer by frarees · Dec 30, 2013 at 05:40 PM
Depends on how flexible you want it to be. In my case, I don't use to add and remove components that much, so I cache/serialize references to them. Cached references access cost nothing. Normally you would assign at runtime (e.g. Awake
) I found interesting doing that at editor time i.e. serialize cached refs. Here's a basic example to show how it would work (typed directly here, so forgive typos :P)
public class MyBehavior : MonoBehaviour {
[SerializeField, HideInInspector]
Transform m_Transform;
[SerializeField, HideInInspector]
Animator m_Animator;
void Reset () {
m_Transform = transform;
m_Animator = animator;
}
}
In this case I'm taking advantage of the Reset
callback being called when the behavior is instantiated (that won't work if the component was previously there. You may want to implement stuff on OnEnable
and use ExecuteInEditMode, or create a context menu to reassign references... the implementation is up to you).
Some time ago we discussed about it here.
Also, Unity's implementation of GameObject internally have those references serialized (examine a prefab or scene YAML file, or even the UnityEngine
assembly). They just don't expose them in their API.
Thank you frareees, that's not exactly the answer I am looking for :)
I will edit the post to include code to better explain what I mean
GetComponent's implementation resides on the unmanaged code (C++), I know no way to bypass that.
Anyway, I think you could create a base class $$anonymous$$onoBehaviourExtended
that caches every component at runtime via GetComponents (typeof (Component))
(or the generic overload) and make it override casting operator for Component
, yielding the cached component of that desired type (in case it exists), as you show in your example.
Your answer
Follow this Question
Related Questions
How do I fix my game after API failing to update my scripts 2 Answers
How can I memset a Native Array from multiple threads? 0 Answers
Possible to use the Google Custom Search API from within Unity? 1 Answer
NetworkMatch Request Error Out of memory 1 Answer
Unity 5 Adding a script component to another object upon Enter 0 Answers