Button (400473)

Is there any one there can explain how i get this Code

var projectile : Rigidbody; 
var speed = 0;    

function Update () { 
  if (Input.GetButtonDown ("Fire2")) { 
    var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation); 
    Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); 
    instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed)); 
  } 
}

working on a button on iphone

my button code

/* GUI.Button example */

function OnGUI () {
	if (GUI.Button (Rect (25, 25, 100, 30), "Button")) {
		// This code is executed when the Button is clicked
	}
}

thanks in advance

Since there is no button/joypad input to the iPhone, you will need to use screen touches or GUI elements to simulate it. Your code is basically OK by the looks of it. Is there anything in particular you are having trouble with?

Yes my problem is

My game object have the fire script attach on it and working when i use two finger touchs on screen.

But i want to press my button and my object fire

" in short i have object wiht scripts how do i activate those from a button "

:wink: :wink: :wink: :sweat_smile: :sweat_smile:

Any :frowning:

First of all, I suggest you use GUITextures instead of OnGUI/GUI.x since the latter is devastatingly slow on the iPhone.

So first, create a GUITexture (GameObject / Create Other / GUI Texture), then in the Update of a script attached to that GameObject, put something along the lines of

if(iPhoneInput.touchCount > 0)
{
  var touch: iPhoneTouch = iPhoneInput.touches[0];

  if(touch.phase == iPhoneTouchPhase.Ended  guiTexture.HitTest(touch.position))
  {
    // myShootingObjectScript is a public variable set in the inspector to point to the object doing the actual shooting
    myShootingObjectScript.Shoot();
  }
}

And in the target object:

public function Shoot()
{
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
    Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed));
  } 
}

IM HAVING THE EXACT SAME PROBLEM.

a detailed explanation of the solution would be much appreciated

thank you

Yeah, I’ve heard that but I’m not convinced it’s so. See this: http://forum.unity3d.com/viewtopic.php?t=36176

Hm, that sounds a bit strange. In Diceworks, even one GUI.Button caused a noticeable slowdown in the framerate (and it was indeed noticeable even without an FPS counter) on iPod 2g. It was before 1.5 however, so that might have changed some things. I’ll look into this when I have time, and report back to the linked topic when I have some results.

Thanx, I’d sure appreciate that. I would like to reap the benefits of any and all methods of performance increase, as I’m sure everyone would. But if 1.5 provides the same performance for OnGUI and GUITexture then it’ll be good to know that too.

I would strongly suggest that to make a GUI on the iPhone you use one of the working user based systems rather than OnGUI or GUIx.

Do a search for SpriteManager, GUIManager or the one I use: SpriteUI. SpriteManager is the base for GUIManager SpriteUI, but unlike GUIManager SpriteUI, does not have GUI functions “out of the box”.

None are perfect, but with a little effort, you can have a working GUI with one draw call per 1024^2 atlas.

Thanks

for all the input …

it much appreciated

Greetings

Casa :smile:

GUIx does not work on the iphone anyway, so thats out as an option :slight_smile:

Actually for input kind of things you don’t even need buttons, just touch handling and “regions” (-> Rect) + potentially a visual object so they have some kind of indicator should you want that.


i was thinking of setting the bottom right corner region as fire, and just putting a gui image there indicating to press hire for fire. so the image doesn’t do anything, just a visual way of saying press here to fire.

however im not sure how to set the bottom right corner to a fire function. help please

also using spriteui is a possible way to setup a fire button, but again how do i input the fire function in the code?

are there any example projects like fps out there for the iphone?
any example project with a fire button will do

thank you,

I did a test on my iPhone 2g (with old 2.x firware) and lo and behold, OnGUI turned out to be as efficient as using GUITextures/GUITexts. On both the FPS was slightly below 30.

The test scene was quite simple however, having only a full screen background image, two buttons and a fps counter text.

The amount of drawcalls was 6 with both methods, although the number of triangles and vertices was slightly higher when using GUI.x methods.

This contradicts my earlier experiences, so I’d really like to hear from the UT iPhone team if this is some improvement that came with 1.5?

EDIT: I just tried adding a GUI.Button in Diceworks, in which GUI.x functions caused significant slow down before, and it had no effect on the frame rate what so ever.

Arzi:

That’s very interesting. I jumped on SpriteUI when it first came out, as OnGUI was making my game unplayable, and as soon as I moved to a Sprite Manager based solution, I got the responsiveness back. Now, I use OnGUI for nearly all of my NON in-game GUIs, as SpriteUI was getting too heavy on StartUp. I had over 100 individual graphic elements, many layered, that had to be sorted, and it was taking measurable full seconds to process on Awake().

If OnGUI really has been improved in 1.5 (and we don’t know about it!) then I’d like to know that “fer sher”.

My only worry about OnGUI, even if it is more performant, is draw calls. The last time I looked OnGUI would not work well with atlases, and without an atlas, doesn’t that mean lots of small graphics and multiple draw calls?

sunnykhan4u21:

Check out this forum topic:
http://forum.unity3d.com/viewtopic.php?t=22371

Setting up a fire button in SpriteUI (once you have the scripts unpacked and setup) is as simple as:

var shipController : ShipController;	//	Link to call a related function
private var fireButton : Sprite;		//	Var to assign a graphic element

function Start () {
	fireButton 		= DefaultSpriteUI.AddButton (new Vector2 (40, 420), 60, 60, 1, 0, 120, 60, 60, this);
			DefaultSpriteUI.GetSpriteButton (fireButton).mouseOverUV = DefaultSpriteUI.PixelCoordToUVCoord (0, 180);
			DefaultSpriteUI.GetSpriteButton (fireButton).mouseOverUVDimensions = DefaultSpriteUI.PixelSpaceToUVSpace (60, 60);
}

function OnMouseDown (sprite : Sprite, pos : Vector2) {
	if (sprite == fireButton)
		shipController.Fire ();	// Related function Fire ();
//		Fire(); 				// Local function Fire ();
}

All the empty functions are in the script. You just need to set a variable, record the location on the atlas in Start(), and call a fire function either in the MyUIManager script or in an associated script with a cached link.

If you read thru the code it’s very simple:

  • Set up an exposed variable to drag your ShipController script to (assuming you are having a SHIP fire. This could be playerController, or what ever is firing. You could also make a local function that controls firing, but I’d assume that organizationally this would be somewhere else.
  • Set up a private variable that is of type Sprite that will contain your graphic element.
  • Assign your private Sprite variable the location on the iPhone Screen, the size of the displayed button, the layer, the location on the atlas, the size from the atlas. (This is all documented both on the forum and in the script itself behind a layer of //'s)
  • function OnMouseDown (and Up, Moved, Exit - etc.) is already defined in the SampleUIManager script that comes with the package.
  • Add the "if (sprite == myPrivateVar) line and define what it does in code.

Done!

Thank You Little Angel, u hav been very very helpful

Thank you (arzi) for that!

On the iPhone I was having troubles getting my graphical button textures to behave. So, your method gives me the ability to have a “over texture” whenever the touch is on or over the button.

Here’s my coded version of arzi’s ealier approach if it helps anyone:

public var guiUpTexture : Texture2D;
public var guiDownTexture : Texture2D;
public var simulateOnly : boolean = false;
 
function Update()
{
  var bPressed : boolean = false;
  
  if (simulateOnly)
	{
		if (Input.GetMouseButton(0)) // Is mouse button held (not just first down event).
			if (guiTexture.HitTest(Input.mousePosition))
				bPressed = true;
	}
  else if(iPhoneInput.touchCount > 0)
	{
	  var touch: iPhoneTouch = iPhoneInput.touches[0]; 
	  if( guiTexture.HitTest(touch.position) )
	  {
	    bPressed = true;
	  } 
	}  	
   
  if (bPressed  (guiTexture.texture != guiDownTexture))
  {
  	guiTexture.texture = guiDownTexture;
  	// Replace this line with your action call, like:
  	// doSomething();
  }
  else if (guiTexture.texture != guiUpTexture)
  {
  	guiTexture.texture = guiUpTexture;
  }
}

Thanks again,

-Mike C.