This is done by the game where each of your "sub-frame" unit buttons is created by inheriting from the SecureUnitButtonTemplate. Once you've done that, when you click on a unit, the game will make it your target.
The basics of creating a unit button is:
Lua Code:
--Create the unit button:
local f = CreateFrame("Button", "wille480PlayerFrame", UIParent, "SecureUnitButtonTemplate")
-- Tell it which unit to represent (in this case "player":
f:SetAttribute("unit", "player")
-- Tell the game to "look after it"
RegisterUnitWatch(f)
-- Tell it to show the unit's vehicle when the unit is in one:
f:SetAttribute("toggleForVehicle", true)
-- Give it the standard click actions:
f:RegisterForClicks("AnyUp")
f:SetAttribute("*type1", "target") -- Target unit on left click
f:SetAttribute("*type2", "togglemenu") -- Toggle units menu on left click
f:SetAttribute("*type3", "assist") -- On middle click, target the target of the clicked unit
-- Make it visible for testing purposes:
f:SetPoint("CENTER")
f:SetSize(100, 100)
f:SetBackdrop({ bgFile = "Interface\\BUTTONS\\WHITE8X8" })
f:SetBackdropColor(0.3, 0.3, 0.3, 0.6)
-- Then add other objects (such as font strings and status bars), register events (such as UNIT_HEALTH), and add scripts to update the objects in response to the events.
Obviously it needs other "plumbing like health/power bars/texts etc. but that is all up to you as to what you add/leave out from each unit.
If you are looking for a simple (as can be) example of putting this together into a raid frame type configuration using secure group templates , take a loot at
ShotGlass