Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by noradninja · May 13, 2010 at 03:17 PM · iphoneoptimizationsendmessage

Can someone explain SendMessage to me?

I have two objects in a scene, one called Input Manager and the other called Indicator Manager. on Input Manager, I have the following:

//TiltManager.js - Acts as a message controller for all tilt events

private var horizontalOrientation : boolean = true; private var accelerator : Vector3 = iPhoneInput.acceleration;

static var turn : float; static var tilt : float;

var receiver : GameObject; var sensitivity : float = 1.0; var tiltDeadStick : int = 0; var zeroAngle : int = 0;

function Awake () {

if (horizontalOrientation) Screen.SetResolution(480, 320, true); else Screen.SetResolution(320, 480, true);

 } //end Awake

function Update () {

 turn = ((iPhoneInput.acceleration.z * -1) * sensitivity);
 tilt = ((iPhoneInput.acceleration.x - zeroAngle) * sensitivity);


if (turn > tiltDeadStick){ receiver.gameObject.SendMessage ("TiltRight"); } //end if

if (turn < -(tiltDeadStick)){ receiver.gameObject.SendMessage ("TiltLeft"); } //end if

if (tilt > tiltDeadStick){ receiver.gameObject.SendMessage ("TiltForward"); } //end if

if (tilt < -(tiltDeadStick)){ receiver.gameObject.SendMessage ("TiltBack"); } //end if

} //end Update

I set the receiver to the Indicator Manager object. My understanding is that doing this will allow me to send a message from the Tilt Manager (ex. if I tilt the iPhone forward, sends a message "TiltForward") to Indicator Manager, which will then look for a method (function) called TiltForward and execute the contents of that method. This seems to be working so far.

How do I make is so I don't need a specific receiver? I would like to have objects 'subscribe' to events and listen for them; this way I can have multiple objects listen for say a "TiltForward" message and execute different code when they get it. (Ex. Indicator Manager gets the message, fades a graphic in on the screen, while the player GO gets the message and rotates on the X axis along with the movement) As a note, I have looked at the messaging stuff on the wiki; I am looking for an easier to configure solution. Can I just use a GO array for the receiver? Would Unity then iterate through the objects in the array, sending the message to each one? Is this efficient at all or terribly slow?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · May 13, 2010 at 03:34 PM

You can use SendMessageOptions.DontRequireReceiver as the last parameter of your SendMessage to allow sending to an object without a receiver

SendMessage however is pretty slow (in the area of 250 times slower than a normal function call) - i would recommend against using it for an event system if possible

Your idea about a GameObject array however would work, though again, not the fastest of implementations

Comment
Add comment · 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
1

Answer by AndrewGrayGames · Feb 27, 2013 at 07:10 PM

To answer the title question: SendMessage is a means of remotely calling some other game object's method. It's similar in concept to a RPC, only a Message doesn't occur across a network, just on a single agent. The OP's understanding of what a message is is 100% correct.

To answer the actual question, messaging appears to be the wrong approach; the problem of phone tilt causing specific objects to tilt in a specific way sounds like a common behavior that should be attached to all of a certain object, that listens to the Input class for the individual object's behavior. As other posters have noted, Messaging in Unity is known to be slow; having your game objects be associated with a prefab that has this input-listening behavior attached, or programmatically adding the input-listening behavior on object creation are usually going to be significantly more performant and efficient means of getting these objects to respond to mechanical inputs.

For a given object to use a different piece of code on event receipt, I highly recommend using A) C#, and B) a little bit of inheritance to do it, as such:

Note: The below is untested code, as I am writing this at work! This setup may required tweaking to be workable.

Sensor Script

 public class TiltSensor : MonoBehaviour
 {
     #region Variables / Properties

     private TiltResponse _response;

     #endregion Variables / Properties

     #region Engine Hooks

     public void Start() 
     {
         _response = GetComponent<TiltResponse>();
     }

     public void FixedUpdate()
     {
         TiltType turn = DetermineTiltType();
         if(turn > TiltDeadStick)
         {
             _response.OnRightTilt();
         }

         // SNIP: Other cases/actions...
     }

     private DetermineTiltType() 
     {
         // STUB, Bring your own implementation!!
     }

     #endregion Engine Hooks
 }

TiltResponse Base Class:

 public abstract class TiltResponse : MonoBehaviour
 {
     public abstract void OnRightTilt();
     public abstract void OnLeftTilt();
     public abstract void OnUpwardTilt();
     public abstract void OnDownwardTilt();
 }

Specific behavior:

 public class FooBehavior : TiltResponse
 {
     public override void OnTiltRight()
     {
         // Specific actions go here...
     }

     // ...Implement other abstract methods as necessary...

     // ...and because the base inherits from MonoBehaviour, you can
     // still override Start(), FixedUpdate(), Collision/Trigger methods, etc.
 }

This way, objects of a particular type all follow a specific set of rules that is implemented in one place in code, but could be programmatically attached to some newly-spawned prefab if that's the way you're going. As long as a component that fulfills the interface that consists of those tilt checks and the MonoBehaviour interface is on the same game object, you're golden.

Comment
Add comment · 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
0

Answer by burnumd · May 13, 2010 at 03:38 PM

Using SendMessage often is definitely not the fastest method out there. Rather than keeping an array of GameObjects, you can keep an array of functions that all have the same signature.

enum TiltType { LEFT, RIGHT, FORWARD, BACK, }

var listeners : Array = new Array ();

function AddListener (listener : Function) { listeners.Add (listener); }

function Update () { // ... Your tilt detection var tiltType : TiltType;

 if (turn &gt; tiltDeadStick)
 {
     tiltType = TiltType.RIGHT;
 }
 // ... other cases

 for (var listener : Function in listeners)
 {
     listener (tiltType);
 }

}

function HandleTilt (tiltType : TiltType) { Debug.Log ("Recieved tilt: " + tiltType); }

You see here that you can pass whatever function you'd like as long as it takes a TiltType as the argument. Then the function decides whether or not and how to handle the message.

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

(2 hidden skinned meshes) or (1 extra draw call) for character props? 2 Answers

Game build works fine in Android but is jittery in iPhone. 0 Answers

Can an object script NOT contain Update()? 1 Answer

Choice of rendering mode particles 2 Answers

Is "PlayerPrefs" (Get/Set) slow ? 2 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