Tag: Script

Generate Project Folders Script

When creating a new project, use this simple script to generate all your folders:

// generate folders in our project
import System.IO;

@MenuItem ("Project Tools / MakeFolders #&_z")
// menuItem reads the first static function
static function MakeFolder()
{
GenerateFolders();
}

static function GenerateFolders ()
{

var projectPath : String = Application.dataPath + "/"; // store the path for the folders

// creating the folders
Directory.CreateDirectory(projectPath + "Audio");
Directory.CreateDirectory(projectPath + "Materials");
Directory.CreateDirectory(projectPath + "Meshes");
Directory.CreateDirectory(projectPath + "Resources");
Directory.CreateDirectory(projectPath + "Scripts");
Directory.CreateDirectory(projectPath + "Shaders");
Directory.CreateDirectory(projectPath + "GUI");
Directory.CreateDirectory(projectPath + "Textures");
Directory.CreateDirectory(projectPath + "Scenes");
Debug.Log ("Folders Created");

AssetDatabase.Refresh ();

}

Selection Script for Unity3D

This script is royalty free; you can use it for whatever you want to. What it does is when you click an object, the object is highlighted. When you click on it again, the object is un-highlighted, back to normal.

Just copy and paste this script into your JavaScript in Unity. Then attach the script to any visible object in your scene, including 3d text.

Here it is:

// Declare our variables...

var selectedColor : Color = Color(0.2, 0.3, 0.4); //This will give us a color wheel to choose a color from.
private var isSelected : boolean = false; // Simple boolean variable. Can be either true or false.

// Okay, now for the functions.
// The function OnMouseDown means to only do whats in the function when the user clicks the mouse.

function OnMouseDown ()
{
//isSelected = isNotSelected...
isSelected = !isSelected;

// If it's unselected, make sure the material color is white.
if (!isSelected) {
renderer.material.color = Color.white;

}

// If it's selected, change the material color to the selectedColor.
if (isSelected) {
renderer.material.color = selectedColor;

}

}

// Feel free to use this script in commercial or non-commercial purposes.
// My name in the credits somewhere would be appreciated, though.

Moving Objects Through Scripting in Unity3D

You can make objects move in Unity3D without animating them. Use JavaScript! Or C#, but for right now I’m going to show you how to do it with JavaScript. That’s what most Unity developers use.

So, first create a new JavaScript and open it up. Type this in it: (don’t copy and paste, you won’t learn)

function Update () {
transform.Rotate (1,0,0);
}

Save that, and attach it to a GameObject. Play the game, and you will see that whatever object you have attached the script to will be rotating in the x axis. The speed of this rotation is dependent upon the frame per second of your game. So if you have a really fast computer, the object will probably be spinning quite fast.

A few tips:

Transform means location or position.
transform.Rotate means rotate the position.
That (1,0,0) means rotate in the x axis; not y, not z.
Don’t forget the semi colon at the end of line 2.

So how about we make the object rotate at a fixed speed? That’s easy enough. Type this up:

var rotateSpeed: int=4;

function Update () {
transform.Rotate(Vector3.right*Time.deltaTime*rotateSpeed);
}

A few tips:

var means variable
int means integer
Time.deltaTime means, basically, time.
Multiply that by our variable, rotateSpeed (use the asterisk)

Make sure you save and update the script and all that and that, and you’re ready to go! If you want to change the axis of rotation, change Vector3.right to Vector3.up or Vector3.left or whatever.

Done!