Lists and Dictionaries

Revisado con versión: 4.1

-

Dificultad: Intermedio

How to create and use the List and Dictionary collections.

Lists and Dictionaries

Intermedio Scripting

BadGuy

Code snippet

using UnityEngine;
using System.Collections;
using System; //This allows the IComparable Interface
//This is the class you will be storing
//in the different collections. In order to use
//a collection's Sort() method, this class needs to
//implement the IComparable interface.
public class BadGuy : IComparable<BadGuy>
{
public string name;
public int power;
public BadGuy(string newName, int newPower)
{
name = newName;
power = newPower;
}
//This method is required by the IComparable
//interface.
public int CompareTo(BadGuy other)
{
if(other == null)
{
return 1;
}
//Return the difference in power.
return power - other.power;
}
}

#pragma strict
import System; // This allows the IComparable Interface.
// This is the class you will be storing
// in the different collections. In order to use
// a collection's Sort() method, this class needs
// to implement the IComparable interface.
public class BadGuy implements IComparable.<BadGuy>
{
public var name : String;
public var power : int;
public function BadGuy (newName : String, newPower : int)
{
name = newName;
power = newPower;
}
// This method is required by the IComparable interface.
public function CompareTo (other : BadGuy)
{
if (other == null)
{
return 1;
}
// Return the difference in power.
return power - other.power;
}
}

import UnityEngine
import System.Collections
import System
//This allows the IComparable Interface
//This is the class you will be storing
//in the different collections. In order to use
//a collection's Sort() method, this class needs to
//implement the IComparable interface.
public class BadGuy(IComparable[of BadGuy]):
public name as string
public power as int
public def constructor(newName as string, newPower as int):
name = newName
power = newPower
//This method is required by the IComparable
//interface.
public def CompareTo(other as BadGuy) as int:
if other is null:
return 1
//Return the difference in power.
return (power - other.power)

SomeOtherClass

Code snippet

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SomeOtherClass : MonoBehaviour
{
void Start ()
{
//This is how you create a Dictionary. Notice how this takes
//two generic terms. In this case you are using a string and a
//BadGuy as your two values.
Dictionary<string, BadGuy> badguys = new Dictionary<string, BadGuy>();
BadGuy bg1 = new BadGuy("Harvey", 50);
BadGuy bg2 = new BadGuy("Magneto", 100);
//You can place variables into the Dictionary with the
//Add() method.
badguys.Add("gangster", bg1);
badguys.Add("mutant", bg2);
BadGuy magneto = badguys["mutant"];
BadGuy temp = null;
//This is a safer, but slow, method of accessing
//values in a dictionary.
if(badguys.TryGetValue("birds", out temp))
{
//success!
}
else
{
//failure!
}
}
}

#pragma strict
import System.Collections.Generic;
public class SomeOtherClass extends MonoBehaviour
{
function Start ()
{
// This is how you create a Dictionary. Notice how this takes
// two generic terms. In this case you are using a string and
// a BadGuy as your two values.
var badguys = new Dictionary.<String, BadGuy>();
var bg1 = new BadGuy("Harvey", 50);
var bg2 = new BadGuy("Magneto", 100);
// You can place variables into the Dictionary with the Add method.
badguys.Add("gangster", bg1);
badguys.Add("mutant", bg2);
var magneto = badguys["mutant"];
var temp = null;
// This is a safer, but slow, method of accessing values in a dictionary.
// Unityscript dictionaries do not support out parameters like C#, so
// TryGetValue is not available, this is equivalent.
if (badguys.ContainsKey("birds"))
{
temp = badguys["birds"];
}
else
{
// Failure!
}
}
}

import UnityEngine
import System.Collections
import System.Collections.Generic
public class SomeOtherClass(MonoBehaviour):
private def Start():
//This is how you create a Dictionary. Notice how this takes
//two generic terms. In this case you are using a string and a
//BadGuy as your two values.
badguys as Dictionary[of string, BadGuy] = Dictionary[of string, BadGuy]()
bg1 = BadGuy('Harvey', 50)
bg2 = BadGuy('Magneto', 100)
//You can place variables into the Dictionary with the
//Add() method.
badguys.Add('gangster', bg1)
badguys.Add('mutant', bg2)
magneto as BadGuy = badguys['mutant']
temp as BadGuy = null
//This is a safer, but slow, method of accessing
//values in a dictionary.
if badguys.TryGetValue('birds', temp):
pass
else:
pass
//success!
//failure!