0

在我的游戏中,我有一个丰富的收藏品,如果玩家已经收集了它并返回场景,我希望每个单独的收藏品都不会重生。例如,一个场景中可能有一百多个这样的收藏品,如果玩家收集其中一个,离开场景然后返回,他们收集的那个不会重生,但其余的会重生。

经过一些研究,我认为我应该使用数据序列化来存储哪些收藏品已被收集,哪些尚未收集,但我不确定如何去做,而且这个概念对我来说很新。有人可以向我解释一下我应该做什么吗?

谢谢

4

1 回答 1

1

使用 PlayerPrefs 存储播放器的当前状态。

这些是选项:

  1. PlayerPrefs 不支持布尔值,因此您可以使用整数(0 为假,1 为真)并为每个收藏品分配一个整数。优点:更容易实现。缺点:如果存储的对象数量非常多并且需要更多的读/写工作量,则使用太多内存。

  2. 将一个整数分配给多个收藏品并按位存储并来回转换。优点:大量存储对象的内存更少,读/写速度更快。缺点:将每个整数的个数限制为 int32 中的位数。那是32。

  3. 为所有收藏品分配一个大字符串并来回转换。优点:您可以存储除真/假以外的更多状态,也可以使用任何加密来保护数据免受黑客攻击。缺点:我什么都想不到。

选项1:

//store
PlayerPrefs.SetKey("collectible"+i, isCollected?1:0);

//fetch
isCollected = PlayerPrefs.GetKey("collectible"+i, 0) == 1;

按位:

int bits = 0;//note that collectiblesGroup1Count cannot be greater than 32
for(int i=0; i<collectiblesGroup1Count; i++) 
    if(collectibleGroup1[i].isCollected)
       bits |= (1 << i);

//store
PlayerPrefs.SetKey("collectiblesGroup1", bits);

//fetch
bits = PlayerPrefs.GetKey("collectiblesGroup1", 0);//default value is 0
for(int i=0; i<collectiblesGroup1Count; i++) 
    collectibleGroup1[i].isCollected = (bits && (1 << i)) != 0;

字符串方法:

string bits = "";//consists of many C's and N's each for one collectible
for(int i=0; i<collectiblesCount; i++) 
    bits += collectibleGroup1[i].isCollected ? "C" : "N";

//store
PlayerPrefs.SetKey("collectibles", bits);

//fetch
bits = PlayerPrefs.GetKey("collectibles", "");
for(int i=0; i<collectiblesCount; i++) 
    if(i < bits.Length)
        collectible[i].isCollected = bits[i] == "C";
    else
        collectible[i].isCollected = false;
于 2016-12-10T12:42:18.437 回答