Hello Guest

Author Topic: How to access animations/sprites through code?  (Read 3714 times)

Mikael1987

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
How to access animations/sprites through code?
« on: June 30, 2013, 10:50:44 pm »
I'm trying to build a dictionary for a small database of items I need in my game,the idea is the following:

Code: [Select]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ItemClass : MonoBehaviour {

public string name;
public string description;
public int id;
public int uses;
public tk2dSpriteAnimator animations;
public tk2dSprite sprite;

public ItemClass(string name,string description,int id,int uses,tk2dSpriteAnimator animations,tk2dSprite sprite)
{

this.name = name;
this.description = description;
this.id = id;
this.uses = uses;
this.animations =animations;
                this.sprite = sprite;

}
}

public class ItemsDatabase : MonoBehaviour {

public Dictionary <string,ItemClass> dictionary = new Dictionary <string, ItemClass>();

public ItemClass carraca = new ItemClass("Carraca de la Folga","Brilla como un dia festivo",0,1,null,null);
public ItemClass silbato = new ItemClass("Silbato Noctámbula del Cónclave","Un Silbato para unirlos a todos en El Pálpito",0,2,null,null);

void start ()
{
dictionary.Add(carraca.name,carraca);
dictionary.Add(silbato.name,silbato);
}

}


I want to be able to have a 2dtksprite for display on the inventory and a tk2dSpriteAnimator for when I hover start an animation, my question is how can i reference the individual sprites/animations for my ItemClass to accomplish this?,thanks in advanced.
« Last Edit: June 30, 2013, 10:53:38 pm by Mikael1987 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to access animations/sprites through code?
« Reply #1 on: June 30, 2013, 11:20:04 pm »
That depends on how you want to do it. You can store references in your parent class, and then use that to spawn these sprites, or you can use Resources.Load to load the sprite collection. Both options work, but what to do specifically depends very much on what works best for you.