(统一)销毁当前场景中的游戏对象,并使其在加载当前场景后永久销毁?

时间:2019-07-24 09:53:57

标签: c# unity3d

如何永久销毁gameObject?也就是说,如果我要在当前场景中销毁游戏对象,那么我只希望它在那之前存在一次,然后在重新加载同一场景时,我希望它被禁用/销毁。我该如何实现?

编辑:还是下面的脚本只能通过bool变量运行一次?

<?php

// Define our JSON locations object with our locations
$locations = '{"data":[{"id":"241","type":"country","attributes":{"code":"AF","name":"Afghanistan","lat":"33.93911","lng":"67.709953"}},{"id":"235","type":"country","attributes":{"code":"AL","name":"Albania","lat":"41.153332","lng":"20.168331"}},{"id":"236","type":"country","attributes":{"code":"DZ","name":"Algeria","lat":"28.033886","lng":"1.659626"}}]}';

// Decodes the JSON object and prints it out pretty-like
//print_r(json_decode($locations, true));

// Decode the JSON object to a PHP array
$locations = json_decode($locations, true);

// Loop through the newly created PHP array
$parsedLocations = [];

foreach ($locations['data'] AS $location)
{
  // Prints out the current location pretty-like
  //print_r($location);

  $parsedLocations[] = array(
    'id' => $location['id'],
    'type' => $location['type'],
    'code' => $location['attributes']['code'],
    'name' => $location['attributes']['name'],
    'lat' => $location['attributes']['lat'],
    'lng' => $location['attributes']['lng']
  );
}

print_r($parsedLocations);

1 个答案:

答案 0 :(得分:0)

我通过在函数中使用DontDestroyOnLoad()做到了

  

void Awake()

static bool created = false;
public bool Loading = true;

void Awake()
{
  if(!created)
  {
    DontDestroyOnLoad(this.gameObject);
    created = true;
  }
  else
  {
  Destroy(this.gameObject);
  }
}

void Update()
{
  if(Input.GetKeyDown(KeyCode.A) && LoadingOnce == true)
  {
    GameObject go = GameObject.Find("Cloner");
    Destroy(go);
    LoadingOnce = false;
    SceneManager.LoadScene("Home");
  }
}