【问题标题】:"Sprite Pack" for room themes in level generation. Unity3D“Sprite Pack”用于关卡生成中的房间主题。 Unity3D
【发布时间】:2014-12-04 17:10:15
【问题描述】:

所以我想知道是否有办法将精灵“重新包装”为主题。我得到了一张包含我需要的所有舞台精灵的图片,我基本上想知道我是否可以将它们保留在那张图片中,然后将“链接”或其他东西放在一个单独的包中。 我想过做一个“主题”类,然后创建它的实例来匹配我的主题,并使用硬编码的变量来匹配。但我不敢相信没有更好的方法。 我希望我已经解释得足够清楚了^^

【问题讨论】:

  • 加载图像(resources.loadall)并通过名称中的前缀将精灵分类到不同的容器(例如列表)中可能会起作用。这至少可以避免对所有单个精灵进行硬编码。
  • 或者您创建公共数组/列表并在检查器中将精灵拖入其中。
  • FTR 如果你使用 2dToolkit,这是自动的 - 这也是即使 Unity 现在有 2D,人们仍然使用它的原因之一。

标签: unity3d sprite unity3d-2dtools


【解决方案1】:

我最终为那些感兴趣的人做了这个,而且它似乎有效。虽然它确实让我不得不在代码中编写纹理,但我一直在寻找一种在编辑器/文件夹结构中完成它的方法。

ThemePack t = new ThemePack(Room.Theme.Medbay);
t.walls.Add(sprites[6]);
t.floors.Add(sprites[32]);
t.floors.Add(sprites[66]);
t.floors.Add(sprites[67]);
t.floors.Add(sprites[68]);
t.floors.Add(sprites[69]);

using UnityEngine;
using System.Collections;

public class SpriteCollection {
    private Sprite[] sprites;
    private string[] names;

    public Sprite this[int i] {
        get {
            return sprites[i];
        }
    }

    public SpriteCollection(string spritesheet) {
        sprites = Resources.LoadAll<Sprite>(spritesheet);
        names = new string[sprites.Length];

        for(int i = 0; i < names.Length; i++) {
            names[i] = sprites[i].name;
        }
    }

    public Sprite GetSprite(string name) {
        return sprites[System.Array.IndexOf(names, name)];
    }
}

using UnityEngine;
using System.Collections.Generic;
using Level;

public class ThemePack {
    public List<Sprite> walls;
    public List<Sprite> floors;

    public Room.Theme name {get; set;} //Could use a string instead, but this is an enum already existing.

    private ThemePack() {
    }

    public ThemePack(Room.Theme n, List<Sprite> w, List<Sprite> f) {
        name = n;
        walls = w;
        floors = f;
    }

    public ThemePack (Room.Theme n) : this(n, new List<Sprite>(), new List<Sprite>()) {
    }

    public Sprite GetRandomWall() {
        return walls[Random.Range(0, walls.Count - 1)];
    }

    public Sprite GetRandomFloor() {
        return floors[Random.Range(0, floors.Count - 1)];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多