Hi Guys!
What about a free inventory script? If you find bugs, just tell me!
Inventory.js
var manager :inv_manager;
var fieldContents :int[];
var quickContents :int[];
var lines :int = 5;
var perLine :int = 10;
var size :Vector2;
var spacing :Vector2;
private var left :int;
private var top:int;
var draggedItem :int;
function Start(){
fieldContents = new int[lines * perLine + perLine];
quickContents = new int[perLine];
manager.hovered = new boolean[fieldContents.Length];
}
function Update(){
left = (Screen.width / 2) - (perLine * size.x + (perLine - 1)*spacing.x)/2;
top = (Screen.height / 2) - (lines*size.y + (lines - 1)*spacing.y + size.y*2 + spacing.y)/2;
if(left < 0){
left = 0;
}
if(top < 0){
top = 0;
}
if(!manager.mouseOver(Rect(left,top,(perLine * size.x + (perLine - 1)*spacing.x),(lines*size.y + (lines - 1)*spacing.y + size.y*2 + spacing.y)))){
if(draggedItem != 0){
if(Input.GetKeyDown("mouse 0") || Input.GetKeyDown("mouse 1")){
draggedItem = 0;
}
}
}
}
function OnGUI(){
var i :int = 0;
for(var line:int = 0; line<lines; line++){
for(var n :int = 0; n<perLine; n++){
var position_x :int = n*size.x + n*spacing.x + left;
var position_y :int = line*size.y + line*spacing.y + top;
if(fieldContents[i] != 0 fieldContents[i] <= manager.textures.Length){
//field with content
if(GUI.Button(Rect(position_x, position_y, size.x, size.y), manager.textures[fieldContents[i]-1])){
manager.playClickSound(manager.clickSound2);
}
if(manager.mouseOver(Rect(position_x, position_y, size.x, size.y))){
if(draggedItem == 0){
if(Input.GetKeyDown("mouse 1")){
draggedItem = fieldContents[i];
fieldContents[i] = 0;
}
}
}
}else{
//empty field
if(GUI.Button(Rect(position_x, position_y, size.x, size.y), "")){
manager.playClickSound(manager.clickSound2);
}
if(manager.mouseOver(Rect(position_x, position_y, size.x, size.y))){
if(Input.GetKeyDown("mouse 0")){
if(draggedItem != 0){
fieldContents[i] = draggedItem;
draggedItem = 0;
}
}
if(!(manager.hovered[i])){
if(Input.GetKeyDown("mouse 1")){
if(draggedItem != 0){
fieldContents[i] = draggedItem;
draggedItem = 0;
}
}
}
}
}
manager.mouseOver(Rect(position_x, position_y, size.x, size.y),i);
i++;
}
}
for(var y :int = 0; y<perLine; y++){
var p_y = lines*size.y + lines*spacing.y + size.y + top;
var p_x :int = y*size.x + y*spacing.x + left;
GUI.Button(Rect(p_x, p_y, size.x, size.y), (y+1).ToString());
}
if(draggedItem != 0){
GUI.DrawTexture(Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,size.x - 5,size.y - 5), manager.textures[draggedItem-1], ScaleMode.StretchToFill, true, 10.0f);
}
}
function Save(name :String){
if(name == null){
name = "Current";
}
for(var i :int=0; i<fieldContents.Length; i++){
PlayerPrefs.SetInt(name + "_inv["+i+"]", fieldContents[i]);
}
print("Sucessfully saved!");
}
function Load(name :String){
if(name == null){
name = "Current";
}
for(var i :int=0; i<fieldContents.Length; i++){
if(PlayerPrefs.GetInt(name + "_inv["+i+"]") != null){
fieldContents[i] = PlayerPrefs.GetInt(name + "_inv["+i+"]");
}
}
print("Sucessfully loaded!");
}
inv_manager.js
#pragma strict
var clickSound1 :AudioClip;
var clickSound2 :AudioClip;
var clickSound3 :AudioClip;
var hoverSound1 :AudioClip;
var hoverSound2 :AudioClip;
var hovered :boolean[];
var textures :Texture[];
function playRandomHoverSound(){
var ran :float = Random.value;
if(ran <= 1 ran >= 0.5){
audio.clip = hoverSound1;
audio.Play();
}else{
audio.clip = hoverSound2;
audio.Play();
}
}
function playHoverSound(a :AudioClip){
audio.clip = a;
audio.Play();
}
function mouseOver(rect :Rect){
if(Input.mousePosition.x >= rect.x Input.mousePosition.x <= rect.x + rect.width){
if(Screen.height - Input.mousePosition.y >= rect.y Screen.height - Input.mousePosition.y <= rect.y + rect.height){
return true;
}
}else{
return false;
}
}
function mouseOver(rect :Rect, i :int){
if(Input.mousePosition.x >= rect.x Input.mousePosition.x <= rect.x + rect.width){
if(Screen.height - Input.mousePosition.y >= rect.y Screen.height - Input.mousePosition.y <= rect.y + rect.height){
if(hovered[i] == false){
hovered[i] = true;
return true;
}else{
hovered[i] = true;
return false;
}
}
}else{
hovered[i] = false;
return false;
}
}
function playClickSound(a :AudioClip){
audio.clip = a;
audio.Play();
}