Skip to content

Instantly share code, notes, and snippets.

@wmiller
wmiller / Events.cs
Last active October 22, 2023 06:39
Unity3D Event System
using System.Collections;
using System.Collections.Generic;
public class GameEvent
{
}
public class Events
{
static Events instanceInternal = null;
@wmiller
wmiller / gist:8661658
Created January 28, 2014 03:06
XML Database for Unity (Part 1) PlaceTile Method 1
public void PlaceTile(x, y, height)
{
string path;
if (height == 0)
{
path = "Tiles/WaterTile";
}
else if (height == 1)
{
@wmiller
wmiller / Build Map Method
Last active January 4, 2016 18:29
XML Database for Unity (Part 1) Build Map Method 1
public void BuildMap(int width, int height)
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
// Get a noise value
float noiseX = (float)x / width * 6.0f;
float noiseY = (float)y / height * 6.0f;
float noise = Mathf.PerlinNoise(noiseX, noiseY);
@wmiller
wmiller / Infos.cs
Created February 4, 2014 13:55
XML Database part 2 Infos.cs
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
[XmlRoot]
public sealed class TileInfo : DatabaseEntry
{
[XmlElement]
public int Height { get; private set; }
@wmiller
wmiller / AsyncMapBuilder.cs
Created February 3, 2014 21:20
XML Database AsyncMapBuilder.cs
public class AsyncMapBuilder : MapBuilder
{
protected override void InitMapObjects()
{
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
int height = HeightField[y * Width + x];
int tempX = x;
@wmiller
wmiller / ResourceManager.cs
Created February 3, 2014 21:19
XML Database ValidateAssetBundleAssetsInternal
private static void ValidateAssetBundleAssetsInternal()
{
// Get all asset infos from the database and iterate over them
AssetInfo[] assetInfos = Database.Instance.GetEntries<AssetInfo>();
foreach (AssetInfo assetInfo in assetInfos)
{
// Load the object referenced by the asset info. Warn if the
// object fails to load
Object asset = Resources.LoadAssetAtPath<Object>(assetInfo.Path);
if (!asset)
@wmiller
wmiller / ResourceManager.cs
Created February 3, 2014 21:18
XML Database LoadBundlesCo
private IEnumerator LoadBundlesCo(System.Action onComplete)
{
// Get bundle infos from the database
AssetBundleInfo[] bundleInfos = Database.Instance.GetEntries<AssetBundleInfo>();
// Load each bundle using the path in the bundle info
foreach (AssetBundleInfo info in bundleInfos)
{
string url = "file://" + AssetBundlePath + "/" + info.Name + ".unity3d";
@wmiller
wmiller / ResourceManager.cs
Created February 3, 2014 21:17
XML Database LoadAssetAsyncCo
private IEnumerator LoadAssetAsyncCo<T>(AssetInfo info, System.Action<T> onComplete) where T : UnityEngine.Object
{
if (info.AssetBundleInfoRef != null)
{
// Get the bundle from the loaded bundles collection
AssetBundle bundle;
if (!loadedBundles.TryGetValue(info.AssetBundleInfoRef.Entry.DatabaseID, out bundle))
{
throw new System.Exception("Asset bundle not found: " + info.AssetBundleInfoRef.Entry.Name);
}
@wmiller
wmiller / ResourceManager.cs
Created February 3, 2014 21:15
XML Database BuildAssetBundles
[MenuItem("Assets/Build AssetBundles")]
private static void BuildAssetBundles()
{
ClearAssetBundles();
// You would probably want to parameterize this part
Database.Instance.ReadFiles(AssetInfoXMLPath);
// Validate asset infos
ValidateAssetBundleAssetsInternal();
@wmiller
wmiller / AssetData.xml
Created February 3, 2014 21:13
XML Database AssetData.xml
<Database>
<!-- Asset Bundle Infos -->
<AssetBundleInfo ID="ASSET_BUNDLE_MAIN_CONTENT">
<Name>MainContent</Name>
<URL>StreamingAssets/AssetBundles/MainContent.unity3d</URL>
</AssetBundleInfo>
<!-- Asset Infos -->