Search Unity

z_BrushModePrefab.cs (249,37): warning CS0618

Discussion in 'World Building' started by fwalker, Oct 20, 2019.

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I would like to resolve a warning for obsolete API `UnityEditor.PrefabUtility.ConnectGameObjectToPrefab(UnityEngine.GameObject, UnityEngine.GameObject)' is obsolete: `Use RevertPrefabInstance. Prefabs instances can no longer be connected to Prefab Assets they are not an instance of to begin with.'

    We can't upgrade to the latest PolyBrush because our game runs extremely choppy after we upgrade. We will wait until we upgrade to 2019 (we are using Unity 2018.4.3f1) before getting the latest package and removing the old Polybrush.

    The code in question is this:

    Code (CSharp):
    1.     private void PlaceGameObject(z_RaycastHit hit, GameObject prefab, z_BrushTarget target, z_BrushSettings settings)
    2.         {
    3.             if(prefab == null)
    4.                 return;
    5.  
    6.             Ray ray = RandomRay(hit.position, hit.normal, settings.radius, settings.falloff, settings.falloffCurve);
    7.  
    8.             z_RaycastHit rand_hit;
    9.  
    10.             Vector3[] vertices = target.editableObject.editMesh.vertices;
    11.             int[] triangles = target.editableObject.editMesh.GetTriangles();
    12.  
    13.             if( z_SceneUtility.MeshRaycast(ray, vertices, triangles, out rand_hit) )
    14.             {
    15.                 float pivotOffset = placeWithPivot ? 0f : GetPivotOffset(prefab);
    16.  
    17.                 Quaternion rotation = Quaternion.FromToRotation(Vector3.up, target.transform.TransformDirection(rand_hit.normal));
    18.                 Quaternion random = Quaternion.AngleAxis(Random.Range(0f, 360f), Vector3.up);
    19.  
    20.                 GameObject inst = PrefabUtility.ConnectGameObjectToPrefab(Instantiate(prefab), prefab);
    21.  
    22.                 inst.transform.localPosition = target.transform.TransformPoint(rand_hit.position);
    23.                 inst.transform.localRotation = rotation * random;
    24.                 inst.name = FormatInstanceName(prefab);
    25.                 inst.transform.position = inst.transform.position + inst.transform.up * pivotOffset;
    26.  
    27.                 if( avoidOverlappingGameObjects && TestIntersection(inst) )
    28.                 {
    29.                     Object.DestroyImmediate(inst);
    30.                     return;
    31.                 }
    32.  
    33.                 if( hitSurfaceIsParent )
    34.                     inst.transform.SetParent(target.transform);
    35.  
    36.                 PrefabUtility.RecordPrefabInstancePropertyModifications(inst);
    37.  
    38.                 instances.Add(inst);
    39.  
    40.                 Undo.RegisterCreatedObjectUndo(inst, UndoMessage);
    41.             }
    42.         }
    The resolution reads:
    `Use RevertPrefabInstance. Prefabs instances can no longer be connected to Prefab Assets they are not an instance of to begin with.'

    That seems to indicate that we can substitute this:

    Code (CSharp):
    1.   GameObject inst = PrefabUtility.ConnectGameObjectToPrefab(Instantiate(prefab), prefab);
    For this:

    Code (CSharp):
    1.  PrefabUtility.RevertPrefabInstance(prefab, InteractionMode.AutomatedAction);
    And the question remains what to do with the rest of the code that seems to modify properties in a GameObject instance etc...
     
  2. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255