Showing posts with label Unity3d. Show all posts
Showing posts with label Unity3d. Show all posts

Dec 12, 2013

[Unity3d] GUI Button Image

  1. public Texture2D center;
  2.  
  3. void Start() {
  4. ...
  5. center = (Texture2D) Resources.Load("center", typeof(Texture2D)); // Assigns a texture named "center.png" to a Texture2D(center).
  6.  
  7. ...
  8. }
  9.  
  10. void OnGUI() {
  11. ...
  12. if (GUI.Button (new Rect (850, 570, 32, 32), new GUIContent(center))) {
  13. ...
  14. }
  15. ...
  16. }

[Unity3d] Background Image

41
You're almost there, with the 2nd camera approach. Here's how to do it:
  • Create a new camera (GameObject -> Create Other -> Camera), and name it "Background Camera".
  • Create a new GUI Texture (GameObject -> Create Other -> GUI Texture), and name it "Background Image".
  • Click the "Layer" dropdown menu in the Background Image's inspector pane, and select "Add Layer".
  • In the next free "User Layer" slot, create a new layer name called "Background Image". This will be directly under the layer named "Terrain" if you haven't yet added any others.
  • Select your Background Image in the hierarchy, and give it the desired texture, and set the x, y, width and height under "Pixel Inset" so that it fills the screen appropriately.
  • Near the top of the inspector window, Use the layer dropdown menu to assign your "Background Image" layer that you defined earlier to this gameobject.
  • Now select your Background Camera in the hierarchy, and adjust these settings in the inspector:
    • Un-Check Flare Layer and Audio Listener (but leave GUILayer enabled)
    • Set Clear Flags to Solid Color
    • Set Depth to -1
    • Set Culling Mask, first to "Nothing", and then to "Background Image"
  • Now Select you other (main) camera, and in the inspector:
    • Set its Clear Flags to "Depth Only"
    • Click its culling mask setting, and un-check "Background Image". This should result in the culling mask displaying as "Mixed ..."
Voila, this should give you your GUI Texture rendered by your background camera, behind everything else rendered by your main camera. And for any other additional cameras (eg, other camera angles) that you want to use, just repeat the last two steps on them.

Original Text: 

Oct 8, 2013

[Unity3d] How to access variables from other scripts

This is based on Unity 4.1. It might be different depending on Unity versions. 
Suppose that you want to access aaa.js from bbb.js. 

aaa.js

var nSource : int;  //  It doesn`t have to be  static, but private is not allowed here. 

bbb.js 

var src  : aaa;
function Start ()
{
      src =  FindObjectOfType(aaa);
}

Usage: 
function customFunction()
{
        var a = aaa.nSource; 
}

[Unity3d] How to convert 3d coords to 2d GUI position

You can use WorldToScreenPoint function. 

var screenPos : Vector3 = GameObject.FindWithTag("MainCamera").camera.WorldToScreenPoint( transform.position); 


[Unity3d] C++ dll for iPhone and XBox 360

#if UNITY_IPHONE || UNITY_XBOX360

   // On iOS and Xbox 360 plugins are statically linked into
   // the executable, so we have to use __Internal as the
   // library name.
   [DllImport ("__Internal")]

   #else

   // Other platforms load plugins dynamically, so pass the name
   // of the plugin's dynamic library.
   [DllImport ("PluginName")]

   #endif
iOS and X-Box use dll as a static library. So, you should use "__internal" when importing. 

[Unity3d] How to use C++ dll in Unity3d

1. Make CLI  Class library project in Visual Studio. 


2.  Delete all files except main cpp  file. You don`t need other auto created files. 


3. Write down the codes you need like this. 

4.  Drag and drop the dll file to Unity Assets/Plugins. It is recommended that you use release mode rather than debug mode while building the project. 
(Make sure you drag and drop to Plugins folder. Other folders  may not work or cause errors.) 
5.  Create a C# script and attach it to a game object. Write code as below.  
 Then you will have value of '1102' in integer variable "nRet". 

[Unity3d] How to change mouse cursor.

1.  Put the png file in  "Assets/Textures/UI/Resources "
2. Write the code as below.   
Then you will see the mouse cursor changed if the mouse button is down. 
Have fun. 

[Unity3d] How to pop up web browser.

Application.ExternalEval("window.open('http://www.google.com','_blank')");  

Caution: Popup prevention option of the web browser must be turned off. 

[Unity3d] How to send message from web browser to Unity Web Player.

It is especially useful, when you start and initialize your unity web player. 
First, you need to write java script in the web side like this. 
 document.getElementById("UnityContent").SendMessage("UnityObject", "UnityFunction", "argument"); 

If you  have Unity Object, then it will be more simple like this 
var u = new UnityObject2(config) ;
u.getUnity().SendMessage("UnityObject", "UnityFunction", "argument"); 

Then make an empty game object in unity editor. In this case,  "UnityObject" is the name of the game object. Add a javascript to the object and write as below. 
function UnityFunction(param : string)
{
      var ret = param;
}

Then your Unity Web Player will have the message from the web server. 
Caution : 
While loading, Unity Web Player can not receive any messages. So, is is possible that the message is ignored. To prevent this, ask the web server to send message after completing loading like this. 
function Start () 
{
    Application.ExternalCall("GetMessage");
}
GetMessage is a user defined function name. And write in your web server as below. 

        function GetMessage()
      {
          u.getUnity().SendMessage("IdObject", "PutPlayerId", "serfefer");
      }

Then, web server will send a message after Unity Web Player is completely loaded, and no message will be lost. 

[Unity3d] How to change scenes without interrupting or pausing the game.

GetStreamProgressForLevel returns progression of the loading. 
You can show the progress bar using this  function. 
If you want to change scenes without interruption, do like this. 

if(Application.GetStreamProgressForLevel("mainui") == 1)
{    
        Application.LoadLevel("mainui");
}  

[Unity3d] How to find camera

If  you can check the tag of the game object, it is as simple as below. 

GameObject.FindWithTag("MainCamera").camera