Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IgorAherne · Dec 30, 2013 at 05:24 PM · getcomponentmemoryapicustompointer

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
           }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spinnernicholas · Dec 30, 2013 at 06:48 PM 0
Share

Is somevarOfTypeGameObj a component or just a variable?

Your example is full of holes.

avatar image IgorAherne · Dec 30, 2013 at 06:57 PM 0
Share

It's a variable of type GameObject. It's value is assigned inside OnEnable()

avatar image Spinnernicholas · Dec 30, 2013 at 07:05 PM 1
Share

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.

avatar image IgorAherne · Dec 30, 2013 at 08:49 PM 0
Share

Spineer, could you give me a hand on this one as well? here

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Dec 30, 2013 at 06:40 PM 0
Share

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

avatar image Spinnernicholas · Dec 30, 2013 at 06:42 PM 0
Share

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.

avatar image Spinnernicholas · Dec 30, 2013 at 07:35 PM 0
Share

Oh, thank you. Was that Unity Threads link helpful?

avatar image IgorAherne · Dec 30, 2013 at 08:33 PM 0
Share

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

avatar image Spinnernicholas · Dec 30, 2013 at 08:53 PM 0
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Dec 30, 2013 at 05:49 PM 0
Share

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

avatar image frarees · Dec 30, 2013 at 07:04 PM 2
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges