Friday, November 15, 2013

UNITY3D Quit application script

/*
    Copyright 2013, Aleksander Hristov, boxcollider@gmail.com .

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using UnityEngine;
using System.Collections;

//Class responsible for quitting application. Shows button in the top right corner of a screen.
//Attach this script to an empty game object in scene.

public class AppQuitter : MonoBehaviour
{

 float screenScale = 0.15f;
 int buttonWidth;
 int buttonHeight;

 void Start ()
 {
  buttonWidth = (int)(Screen.width * screenScale);
  buttonHeight = (int)(Screen.height * screenScale);
 }

 void OnGUI ()
 {
  if (GUI.Button (new Rect (Screen.width - buttonWidth, 0, buttonWidth, buttonHeight), "Quit")) {
   Application.Quit ();
  }
 }
}

Thursday, October 31, 2013

UNITY3D PlayerPrefs viewer window

/*
    Copyright 2013, Aleksander Hristov, boxcollider@gmail.com .

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using UnityEngine;
using System;
using Microsoft.Win32;
using UnityEditor;

//Class responsible for viewing PlayerPrefs in Editor Window .
//Place this script in folder named "Editor".
//Click on menu bar on the top PlayerPrefs->Viewer

public class PlayerPrefsViewer : EditorWindow { 
 
 string[] appKeyNames; 
 string[] appValueData;
 int prefsElementNumber=0;
 Vector2 scrollInitialPosition =Vector2.zero;
 
 [MenuItem ("PlayerPrefs/Viewer")]
 static void Starti () { 
  
    EditorWindow.GetWindow<PlayerPrefsViewer>("PlayerPrefs");
 }
 
 void OnEnable(){
   getPlayerPrefs();
 }
 
 void getPlayerPrefs(){  
  
  RegistryKey appProductName = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey(PlayerSettings.companyName).OpenSubKey(PlayerSettings.productName,true);
  if(appProductName==null){
   return;
  }  
  
  prefsElementNumber=appProductName.ValueCount;  
  
  if(appProductName.ValueCount<=0){
   return;
  }
  
  appKeyNames=new string[ prefsElementNumber];  
  appValueData=new string[ prefsElementNumber];  
    
  appKeyNames=appProductName.GetValueNames();
  
  for(int i=0;i<prefsElementNumber;i++){  
    
     appKeyNames[i] = fromRegistryKeyToUnityKey(appKeyNames[i]);
     appValueData[i]= fromRegistryValueToUnityValue(appKeyNames[i]);
   
  }  
 } 
 
 void OnGUI () {   
  
       if(prefsElementNumber<=0){
   EditorGUILayout.BeginVertical();
   showRefreshButton();
   EditorGUILayout.LabelField("PLAYERPREFS EMPTY");   
   EditorGUILayout.EndVertical();
   return;
  }
  
  EditorGUILayout.BeginVertical(GUILayout.ExpandHeight(true));
  
   scrollInitialPosition=EditorGUILayout.BeginScrollView(scrollInitialPosition,false,true);
      showRefreshButton();
  
   EditorGUILayout.Space();
  
    for(int i=0;i<prefsElementNumber;i++){     
    
     EditorGUILayout.BeginHorizontal();
            
      EditorGUILayout.SelectableLabel(appKeyNames[i]);
             
      EditorGUILayout.SelectableLabel(appValueData[i]);
   
      GUI.backgroundColor=Color.red;
   
      if(GUILayout.Button("delete preference")){       
       PlayerPrefs.DeleteKey(appKeyNames[i]);
        getPlayerPrefs();    
      }
   
     EditorGUILayout.EndHorizontal(); 
    }  
   
   EditorGUILayout.Space();
  
   GUI.backgroundColor=Color.blue;
  
   if(GUILayout.Button("Delete all preferences",GUILayout.ExpandWidth(true))){
      PlayerPrefs.DeleteAll();
       prefsElementNumber=0;
    getPlayerPrefs();
   }
 
   EditorGUILayout.EndScrollView();
  
  EditorGUILayout.EndVertical();  
 }

    void showRefreshButton(){
   GUI.backgroundColor=Color.green;
   if(GUILayout.Button("Refresh player preferences",GUILayout.ExpandWidth(true))){
    getPlayerPrefs();
   }  
 } 
 
 string fromRegistryValueToUnityValue(string prefKey){
  
  if(PlayerPrefs.GetInt(prefKey,int.MinValue)!=int.MinValue){   
   return PlayerPrefs.GetInt(prefKey).ToString();
  }
  if(PlayerPrefs.GetFloat(prefKey,float.MinValue)!=float.MinValue){
   return PlayerPrefs.GetFloat(prefKey).ToString();   
  }  
  return PlayerPrefs.GetString(prefKey);
 }
 
 string fromRegistryKeyToUnityKey(string key){
  return key.Remove(key.LastIndexOf("_"));
 }

}

UNITY3D rotate object script

/*
    Copyright 2013, Aleksander Hristov, boxcollider@gmail.com .

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


using UnityEngine;
using System.Collections;


//Class responsible for rotating an object along Y world axys.
//Attach this script to an empty game object in scene.


public class GameObjectRotator : MonoBehaviour {
  Transform myTransform;
  public float rotationSpeed=10f;

 void Start () {
   myTransform=transform;
 } 

 void Update () {
    myTransform.Rotate(Vector3.up*rotationSpeed*Time.deltaTime,Space.World);
 }
 
    void OnBecameVisible() {
        enabled = true;
    }
 
    void OnBecameInvisible() {
        enabled = false;
    } 
}

UNITY3D reference pages

UNITY3D Manual

UNITY3D Reference Manual

UNITY3D Scripting Reference

UNITY3D Wiki

C# Reference

.NET Framework 2.0 Class Library Reference

-

Unite video archive

UNITY3D tutorials page

UNITY3D live training archive page

Unity 3D Student

Walker Boys Studio

Digital Tutors

3D Buzz

BurgZerg Arcade

Will Goldstone

TornadoTwins

Wednesday, October 30, 2013

UNITY3D fps counter script.

/*
    Copyright 2013, Aleksander Hristov, boxcollider@gmail.com .

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using UnityEngine;
using System.Collections;

//Class responsible for showing current game fps.
//Attach this script to an empty game object in scene.

public class FPSCounter : MonoBehaviour
{
 GUIText fpsText;
 string fpsTitle = "FPS : ";
 Color textColor = Color.red;

 void Start ()
 {
  initGUIText ();
  InvokeRepeating ("trackTime", 1, 1);
 }

 void initGUIText ()
 {
  transform.position = new Vector3 (0.5f, 0.1f, 0);
  fpsText = gameObject.AddComponent<GUIText> ();
  fpsText.material.color = textColor;
  fpsText.fontSize = (int)(Screen.height * 0.07f);
  fpsText.anchor = TextAnchor.MiddleCenter;
  fpsText.alignment = TextAlignment.Center; 
 }
 
 int lastFrameCount = 0;

 void trackTime ()
 {  
  fpsText.text = fpsTitle + (Time.frameCount - lastFrameCount).ToString ();
  lastFrameCount = Time.frameCount;
 }
}