Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Marquee tool
#10
(11-06-2019, 08:38 AM)Richard Wrote: Currently, my code is below;

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Obi
{
   public class CreateHandle : MonoBehaviour
   {
       ObiActor actor;
       public ObiSolver solver;
       public Transform anchor;
       static protected Vector3[] wsPositions = new Vector3[0];
       static public bool[] selectionStatus = new bool[0];
       static int lastSelectedParticle = 0;
       //Selection related:
       static protected int selectedCount = 0;
       public static int SelectedParticleCount
       {
           get { return selectedCount; }
       }
       // Start is called before the first frame update
       void Start()
       {
           ObiSolver solver = actor.Solver;
           actor = GetComponent<ObiActor>();

           for (int i = 0; i < actor.positions.Length; ++i)
           {
               int indexInSolver = actor.particleIndices[i];
               Vector3 projectedPos = GetComponent<Camera>().WorldToScreenPoint(solver.positions[indexInSolver]);
               selectionStatus[i] = true;
           }

           // Create the handle:
           GameObject c = new GameObject("Obi Handle");
           Undo.RegisterCreatedObjectUndo(c, "Create Obi Particle Handle");
           ObiParticleHandle handle = c.AddComponent<ObiParticleHandle>();
           handle.Actor = actor;

           selectedCount = 0;
           for (int i = 0; i < selectionStatus.Length; i++)
           {
               if (actor.active[i] && selectionStatus[i])
               {
                   selectedCount++;
                   lastSelectedParticle = i;
               }
           }

           // Calculate position of handle from average of particle positions:
           Vector3 average = Vector3.zero;
           for (int i = 0; i < selectionStatus.Length; i++)
           {
               if (selectionStatus[i])
               {
                   average += wsPositions[i];
               }
           }

           c.transform.position = average / selectedCount;

           // Add the selected particles to the handle:
           for (int i = 0; i < selectionStatus.Length; i++)
           {
               if (selectionStatus[i])
               {
                   handle.AddParticle(i, wsPositions[i], actor.invMasses[i]);
               }
           }
       }


       // Update is called once per frame
       void Update()
       {

       }
   }
}

There are two errors.
・Obi Solver does not contain definition of "position"
(error CS1061: 'ObiSolver' does not contain a definition for 'positions' and no accessible extension method 'positions' accepting a first argument of type 'ObiSolver' could be found (are you missing a using directive or an assembly reference?))

・I cannot find out "Undo"
(error CS0103: The name 'Undo' does not exist in the current context)


Do you know how to fix this? Or are there other much problems here, or I should change code at a fundamental level?

Hi Richard,

You just copy-pasted code straight out the particle editor (which is editor code, that cannot run in-game), without modifying it. You just cannot use this code as-is in your game, it won't work.

Blindly copy-pasting code won't get you far. You need to understand what you're writing and why. For instance, the selectionStatus array is a per-particle array that holds whether each particle is selected or not. This only makes sense in the context of in-editor selection. The "Undo" class only exists in the UnityEditor namespace, so you cannot use it here.

As I've pointed out multiple times before, you must determine whether you're writing an editor tool, or code for a game. You're constantly mixing up both.
Reply


Messages In This Thread
Marquee tool - by Richard - 09-06-2019, 05:06 PM
RE: Marquee tool - by josemendez - 10-06-2019, 08:32 AM
RE: Marquee tool - by Richard - 10-06-2019, 09:21 AM
RE: Marquee tool - by josemendez - 10-06-2019, 02:19 PM
RE: Marquee tool - by Richard - 10-06-2019, 02:37 PM
RE: Marquee tool - by josemendez - 10-06-2019, 03:44 PM
RE: Marquee tool - by Richard - 10-06-2019, 04:14 PM
RE: Marquee tool - by josemendez - 10-06-2019, 07:21 PM
RE: Marquee tool - by Richard - 11-06-2019, 08:38 AM
RE: Marquee tool - by josemendez - 11-06-2019, 09:04 AM
RE: Marquee tool - by Richard - 11-06-2019, 09:14 AM
RE: Marquee tool - by josemendez - 11-06-2019, 11:06 AM
RE: Marquee tool - by Richard - 11-06-2019, 11:38 AM
RE: Marquee tool - by josemendez - 11-06-2019, 12:02 PM
RE: Marquee tool - by Richard - 11-06-2019, 04:21 PM
RE: Marquee tool - by josemendez - 12-06-2019, 07:13 AM
RE: Marquee tool - by Richard - 12-06-2019, 11:10 AM
RE: Marquee tool - by josemendez - 12-06-2019, 12:23 PM
RE: Marquee tool - by Richard - 12-06-2019, 12:49 PM